Results 1 to 7 of 7
  1. #1
    dollygg is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jul 2009
    Posts
    73

    How to compare date when it is string type?

    I want to set the criteria as start date <= report date in the query. Theses two dates are string type. The cdate () didn't work quite well . How to compare the dates regardless of the regional settings on individual user's computer ? eg. start date is "01/03/2023" (meaning is Jan 03, 2023) and report date is "12/30/2022" ( this is the original data in the table ). Some people's regional setting is dd/mm/yyyy and that cause trouble.

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    You could try this wrapper function from A. Browne http://allenbrowne.com/ser-36.html

    Code:
    Function SQLDate(varDate As Variant) As String
        'Purpose:    Return a delimited string in the date format used natively by JET SQL.
        'Argument:   A date/time value.
        'Note:       Returns just the date format if the argument has no time component,
        '                or a date/time format if it does.
        'Author:     Allen Browne. allen@allenbrowne.com, June 2006.
        If IsDate(varDate) Then
            If DateValue(varDate) = varDate Then
                SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
            Else
                SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
            End If
        End If
    End Function
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    Where are these dates, are they are on a form that runs the report?
    I hope that the dates in the underlying data aren't strings?

    If they are stored as strings I would convert them to date field and then you won't have regional problems to deal with - at least in the data.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    dollygg is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jul 2009
    Posts
    73
    HI,

    I need to do this in an Access query. Can I put '#' & start_Date & '#' < '#' & report_Date & '#'?

  5. #5
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    +1 for just convert them to date data types in the underlying data store.

    If for some reason storing as a sting is absolutely required I'd convert them to YYYY-mm-dd format so you can then at least you can sort and compare them.

    https://www.google.com/search?q=date+utc+format

  6. #6
    dollygg is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jul 2009
    Posts
    73
    Actually CDATE() is working.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,860
    Quote Originally Posted by dollygg View Post
    Actually CDATE() is working.
    Regardless, better to keep dates AS DATES and format them as text when needed.
    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

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

Similar Threads

  1. Replies: 5
    Last Post: 02-20-2018, 07:25 PM
  2. Replies: 2
    Last Post: 10-30-2017, 02:33 PM
  3. Replies: 7
    Last Post: 12-26-2014, 12:17 PM
  4. Replies: 5
    Last Post: 04-03-2012, 07:24 AM
  5. mismatch type compare help needed
    By techexpressinc in forum Queries
    Replies: 3
    Last Post: 02-23-2010, 03:27 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