Results 1 to 11 of 11
  1. #1
    Ambre is offline Novice
    Windows 11 Office 365
    Join Date
    Apr 2023
    Posts
    11

    DLookup on Date/Time field returns text date

    Hi,

    I live in the UK so my computer uses dd/mm/yyyy date format.

    When I use DLookup in a query on a Table with a Date/Time field it returns a string version of the date. This means that a sort on this returned field is not chronological. All dates beginning 01 sort first regardless of month or year.

    Why is this? Do I need to tell DLookup to return the stored format of the date in some way?

    I would appreciate any suggestions to correct this.

    Ambre

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Weird, but try to wrap the DLookup in a CDate (like in CDate(DLookup("YourField","tblTable")).
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by Ambre View Post
    Hi,

    I live in the UK so my computer uses dd/mm/yyyy date format.

    When I use DLookup in a query on a Table with a Date/Time field it returns a string version of the date. This means that a sort on this returned field is not chronological. All dates beginning 01 sort first regardless of month or year.

    Why is this? Do I need to tell DLookup to return the stored format of the date in some way?

    I would appreciate any suggestions to correct this.

    Ambre
    I'm in the UK but can't replicate your issue.
    Can you post an example db
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    What is the backend? Access or sql server? Something else?

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Are you using the Date\Time Extended data type?
    https://www.access-programmers.co.uk...-query.327239/

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    Ambre is offline Novice
    Windows 11 Office 365
    Join Date
    Apr 2023
    Posts
    11
    Hi,

    Thanks for the quick response.

    Wrapping the expression in CDate works, but... throws an error if the looked for date is missing!

    Ambre

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Ambre View Post
    Hi,

    Thanks for the quick response.

    Wrapping the expression in CDate works, but... throws an error if the looked for date is missing!

    Ambre
    So use the NZ() function.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I'd be leery of this approach. First, if you don't use criteria in the lookup (so far OP has not shown their expression being used) you get a random value returned. If its month value is 12 or less for European dates (dd/mm/yyyy) as in 03/11/2023 it should be interpreted as March 11, not November 03 as intended. If the date is 15/03/2023 Access should interpret that as a non-US formatted date and use the value of March 15 as intended. So 2 different interpretations of the same date format.

    see http://allenbrowne.com/ser-36.html
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    It does not produce a string but a date?

    From the Immediate window
    Code:
    ? vartype(dlookup("On_Date","Real_dates","ID=8"))
     7
    7 is Date type?
    https://learn.microsoft.com/en-us/of...rtype-function
    Attached Thumbnails Attached Thumbnails Dates.PNG  
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Yes, the documentation states it returns the value found. If it returns a string, then the field must be text regardless if it looks like dates. I didn't mention that because I focused on the idea of coercing what I figured was a string into a date as suggested by using CDate because in the end, you'd have dates. I also figure that some of the date values would get screwed up during that conversion. So I would not wrap CDate in NZ either if the field is text. If the field is date data type, then perhaps a format has been applied, making the sort look weird for dates.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    Ambre is offline Novice
    Windows 11 Office 365
    Join Date
    Apr 2023
    Posts
    11
    Many thanks to everyone who responded to this post. Your comments have introduced me to aspects of Access that I was totally unaware of.

    I still don't fully understand why access behaves as above, but I have found a work around that works for me. If I wrap the DLookup in NZ() and provide NZ with a date so far in the past eg. #01/01/1600# that it is obviously wrong, everything works fine.

    Regards,
    Ambre

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

Similar Threads

  1. Changing short text field to date/time field
    By fishhead in forum Access
    Replies: 5
    Last Post: 03-25-2020, 01:17 AM
  2. Replies: 5
    Last Post: 09-16-2018, 04:58 PM
  3. Replies: 6
    Last Post: 04-19-2016, 03:58 PM
  4. Replies: 2
    Last Post: 08-03-2014, 09:36 AM
  5. Replies: 9
    Last Post: 04-18-2014, 08:51 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