Results 1 to 9 of 9
  1. #1
    kman42 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    40

    Return DLookup result as date?

    Does DLookup return its result as a string?



    I have a query that uses an DLookup expression to return a date from another table, but when I try to sort on the field, it appears to be sorting it as a string rather than as a date. Is there a way to format the field as a date so it sorts properly?

    Here's my expression:
    DLookup("DateOfEvent","tbl_EventLog","fk_EventID=" & EventID)

    This returns the dates okay, but when I sort them in ascending order I get something like this:

    4/14/2011
    4/20/2011
    4/8/2011

    I can sort them in descending order and they switch, but they obviously aren't ordering by the date which is what made me think the DLookup was returning the result as a string instead of as a date.

    All my date fields are formatted as mm/dd/yyyy as time is not important.

    thanks!

  2. #2
    gnoakes is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2011
    Posts
    2
    Hi,

    I would put a format function around your expression such as...

    FormatDateTime(Dlookup("DateofEvent","tbl-EventLog","fk_EventID="&EventID),2)

    The "2" denotes for a "short date" format.

    Hope that helps,

    George

  3. #3
    kman42 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    40
    It's still not sorting correctly.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Have a look at the CDate() function.

  5. #5
    kman42 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    40
    I just happened upon that and have changed to this code, but still no luck:

    DateOfP: IIf(DLookUp("DateOfEvent","tbl_EventLog","fk_Event ID=1)<>"",CDate(DLookUp("DateOfEvent","tbl_EventLo g","fk_EventID=1)),"")

    Some of the dates are empty because that particular type of event hasn't occurred yet so I have to allow for empty returns as well.

    It's still not working with this setup though.

  6. #6
    kman42 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    40
    I did this:
    DateOfP: CDate(Nz(DLookUp("DateOfEvent","tbl_EventLog","fk_ EventID=1),"0"))

    This returns "12:00:00 AM" for the empty fields and since all the others are strictly dates, I was able to use a WHERE clause to get rid of them. This seems like the wrong way to do things, but I at least managed to obtain the proper set of records in the right order.

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Have you single stepped the code to see what is being returned?

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Just so you understand that *all* DateTime fields have a TIME portion. http://support.microsoft.com/kb/q130514/en-us

  9. #9
    kman42 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    40
    Sort of. Once I had it working with a bare minimum of functions (just using cDate), I started adding other functions to try to get rid of the empties. When I added Iif, it stopped working, so I guess it returns strings as well even if the return statement is cDate. Strange.

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

Similar Threads

  1. Query to return only the row containing the highest date.
    By eric.opperman1@gmail.com in forum Queries
    Replies: 4
    Last Post: 03-22-2011, 08:42 PM
  2. Query return for date ranges.
    By Desstro in forum Queries
    Replies: 2
    Last Post: 09-14-2010, 06:44 AM
  3. how to return the result?
    By lamkee in forum Access
    Replies: 1
    Last Post: 08-10-2010, 10:50 AM
  4. Dlookup to match two criteria and return value
    By randolphoralph in forum Programming
    Replies: 20
    Last Post: 05-20-2010, 12:27 PM
  5. return last updated date for each product
    By jonnyuk3 in forum Queries
    Replies: 0
    Last Post: 06-17-2009, 04:38 AM

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