Results 1 to 5 of 5
  1. #1
    123seven is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2011
    Location
    Aurora, ON Canada
    Posts
    22

    Trouble with query criteria for Date/time to return results for "Today"

    Hi
    I have a date/time field ( "DateAdded") on a data entry form and want the recordset of that form to only show the current day's records. IF I set the query criteria for the forms record source to:

    >=#17/03/2012#
    (today being that date), I get today's records. That's what I want but don't want to create a parameter query asking for that date every time the form closes/opens during the day.

    Date()
    returns no results and I'm wondering if it has to time with the time component.



    >Date()-1
    Returns yesterday's and todays records.

    Is there a simple date function that will work here?

    Thanks for your time.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    If your field does have time component saved, such as 3/12/2012 2:37:00 PM, then yes the filter needs to deal with that. Construct a field in query (or calculated field of table) that drops the time part, like: CDate(Format(fieldname, "m/d/yyyy")). Then apply the Date() function as criteria to this field.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    123seven is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2011
    Location
    Aurora, ON Canada
    Posts
    22
    Hey June7
    thanks for your quick reply. Here's what happened - added new column and expression:

    DateOnly: CDate(Format([DateAdded],"dd/mm/yyyy"))

    returns shortdate, same as [Date Added], was feeling very good at this point, then added the criteria:

    =Date()

    and got a "Data Type Mismatch in Criteria Expression"

    also tried a new column/expression:

    Expr1: DateValue(Format([DateAdded],"Short Date"))

    which returned records in shortdate format as expected. Added the =date() criteria and got the same error msg -- Also can't figure how to see datatype of a calculated field (if that's possible)...thanks again

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    I just tested my suggested expression and it works for me. Wait, you have format of d/m/yyyy, not m/d/yyyy. m/d/yyyy is Access convention. Review http://allenbrowne.com/ser-36.html

    The Format function results in a string so using CDate or DateValue to define a date.

    Is your field name [DateAdded] or [Date Added] - with or without space?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    123seven is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2011
    Location
    Aurora, ON Canada
    Posts
    22
    Hi June7,
    Just wanted to thank you and let you know how this was resolved. I reviewed the Allen Browne article as you suggested and through this worked out my problem. I had two records with null values in the date/time field - To quote Allen Browne :
    "Calculated Date Fields

    Access can also misinterpret calculated date fields in queries, especially where the date format is not American and the field contains some Nulls. The obvious symptoms are that the field is left-aligned and sorted as strings."

    So the nulls as well as the 'Canadian' regional date setting (which is actually ok to use in the query grid (if that's your regional setting, according to Al)) were the trouble. Once I cleared up the null values both calculated fields returned results when queried with the =date() criteria.

    Thanks again!

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

Similar Threads

  1. Replies: 4
    Last Post: 03-14-2012, 09:05 AM
  2. Passing criteria "NULL" or "IS NOT NULL" to a query
    By SgtSaunders69 in forum Forms
    Replies: 1
    Last Post: 12-24-2011, 02:22 AM
  3. Criteria field in query set to "no selection"
    By 4thangel in forum Access
    Replies: 2
    Last Post: 06-24-2011, 12:43 AM
  4. Query Results "Count Totals"
    By tipopilotas in forum Queries
    Replies: 1
    Last Post: 03-26-2010, 10:36 PM
  5. Access Date/Time....I need a "generic" date.
    By beastmaster in forum Access
    Replies: 2
    Last Post: 12-29-2005, 12:55 PM

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