Results 1 to 7 of 7
  1. #1
    jctaylor is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    16

    Date variable in query


    I have a form that has text boxes for beginning date and ending date. I then set these values to public variables and then wrap these variables in a function for each one. In my query I limit the data between the two dates, but when I try to use the values from the function, it returns no records. How do I get the query to use the function value to limit the records? If I just set the values to two different dates and surround them with #01/01/17# that works fine but not if I try to use the value from the function. Thanks for any suggestions.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    select * from table where [dateFld] between forms!myForm!txtStartDate and orms!myForm!txtEndDate

  3. #3
    jctaylor is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    16
    I tried that, but I have the form picking the dates and other criteria on one form and then I want to use those values in running several different reports. I read where you can use a function to get a public variable and use that is a query. I am trying to get dates and maybe it has something to do with how you show them(#01/01/2015#)- variable begdate. Do you use # around the variable?

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    You can use a function in a query by entering the function name, such as DateField: MyFunction()

    This is not normal usage, usually there are parameters being passed. Post the code for your function.

  5. #5
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Read you post again - this is criteria.

    One way would be to return a true or false value from the function;
    Field: MyFunction(begdate,enddate)
    function checks if dates are in the range and sets true or false
    Criteria:=True

    Note however that this is a very slow process. With more than 100 records or so the query will be very slow. The preferred method would be to have the dates on a form, even if they are hidden fields (or the form itself is hidden). Or even on a temporary table. Both will be better than leaving them as variables in the code.

  6. #6
    jctaylor is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    16
    Ok, Thanks. I will try that.

  7. #7
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,799
    Each report has to be based on something, although in this case, a query or sql statement might be better than a set of table records that are filtered. So if each report has its own query (or if preferred, table), open each report from the form and filter each report according to the date range.
    Or pass each sql statement, using the dates as part of the WHERE clause, as the data source for each report as you open it.

    What I'm not getting is, if the date variables are truly global (i.e. you know the difference between a project level vs. form level variable) then I see no need for a function to get a global variable value. It would be like using a device to reach for the salt when it is right under your nose. I have to wonder if the problem is the scope of the "public" variable. You realize that declaring a variable in a form (for example) as Public does not make it visible to any other form, report or module?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 04-27-2015, 02:40 PM
  2. Query with Date Variable
    By robrich22 in forum Queries
    Replies: 1
    Last Post: 02-17-2014, 02:46 PM
  3. Query with Date Variable
    By robrich22 in forum Queries
    Replies: 2
    Last Post: 02-17-2014, 01:03 PM
  4. Set date field on subform to date variable
    By laavista in forum Access
    Replies: 4
    Last Post: 06-30-2010, 06:32 PM
  5. set date field on form to date variable
    By laavista in forum Access
    Replies: 3
    Last Post: 06-28-2010, 03:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums