Results 1 to 14 of 14
  1. #1
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150

    Date in query flipping to US format resulting in incorrect results.

    I am stumped. I had a lovely bit of code working and now it suddenly isn't.

    I think I've located the problem, DueDate is in British format dd/mm/yyyy. When I add the day to it, this works but the SQL query comes back with results as if the date was in US format mm/dd/yyyyy. It was working before because I was testing it on the 29th so the date format was forced to stay British but now it could be either way round it's flipping it to US. I have tried updating my code to format the date but this is making no difference. Please can anybody help?? I'm meant to be demoing this in two hours. Hmm.....

    Code:
    Function isHistoricMateRequest(ConnectorID As Integer, DueDate As Date) As Boolean  
    
      ' Checks to see if there are any completed mates after the due date of the current request       
    Dim rs As DAO.Recordset    
    Dim strWhere As String     
    
      ' Add a day  to the due date.   
     ' The due date has the format dd/mm/yyyy so defaults to dd/mm/yyyy 00:00:00    ' meaning any mates performed on the same day result it it's clasification as a historic request.   
     ' To counter this, add a day to the due date. 
       DueDate = DateAdd("d", 1, DueDate)    
    
        strWhere = "WHERE [Connector1]=" & ConnectorID & " AND [DatePerformed] > #" & Format(DueDate, "dd/mm/yyyy") & "#"    
    
        Set rs = CurrentDb.OpenRecordset("SELECT * FROM qryMatesFullHistory " & strWhere)    
    
        If rs.BOF And rs.EOF Then        
    isHistoricMateRequest = False  
      Else      
      isHistoricMateRequest = True  
      End If
    
    End function

    Last edited by Robyn_P; 07-03-2018 at 08:35 AM. Reason: Formatting

  2. #2
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You have to force the date to US format (or better still true international format) to make it work.
    Review http://allenbrowne.com/ser-36.html

    Try
    Code:
    Function isHistoricMateRequest(ConnectorID As Integer, DueDate As Date) As Boolean    ' Checks to see if there are any completed mates after the due date of the current request    
        Dim rs As DAO.Recordset
        Dim strWhere As String        '
        'Add a day  to the due date.
        ' The due date has the format dd/mm/yyyy so defaults to dd/mm/yyyy 00:00:00
        ' meaning any mates performed on the same day result it it's clasification as a historic request.
        ' To counter this, add a day to the due date.
        
        DueDate = DateAdd("d", 1, DueDate)
        
        strWhere = "WHERE [Connector1]=" & ConnectorID & " AND [DatePerformed] > #" & Format(DueDate, "yyyy/mm/dd") & "#"
        
        Set rs = CurrentDb.OpenRecordset("SELECT * FROM qryMatesFullHistory " & strWhere)
        If rs.BOF And rs.EOF Then
            isHistoricMateRequest = False
        Else
            isHistoricMateRequest = True
        End If
        
    End Function
    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 ↓↓

  3. #3
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    Thanks! I literally just figured that out. Is there an advantage of using yyyy/mm/dd over mm/dd/yyyy? Also I assume I don't have to worry about formatting the actual data. That will know what format the date is in right?

  4. #4
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Access in the query editor handles the dates correctly based on your regional settings.
    However in VBA , in the SQL query they are always treated as being in US format.

    Personally I find the yyyy/mm/dd format is purely easier to spot any mistakes when you are debugging.
    It's not obvious if 03/04/2018 is April 3rd or March 4th in a debug window but 2018/04/03 is definitely April 3rd.

    This also translates well into SQL server '2018-04-03' would always work if you upgraded your back end database.
    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 ↓↓

  5. #5
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    Thanks, that's great. Okay, now to check where else I may have this problem. Unlikely to upgrade the back-end but good to keep in mind if I do

  6. #6
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    If you write lots of SQL on the fly in VBA you can use the SqlDate() function that Allen Browne provide on his website, it is reliable and pretty foolproof.
    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 ↓↓

  7. #7
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    Is that the one copied into this thread? https://access-programmers.co.uk/for...d.php?t=227968 I tried that, the formatting came out weird, but I was only taking snippets of it so maybe that's why. Also possibly a stupid question, but how does it know what format the date is in in the first place? Is that based on regional settings?

  8. #8
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Very similar- I've just changed it a bit;

    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, "\#yyyy\/mm\/dd\#")
            Else
                SQLDate = Format$(varDate, "\#yyyy\/mm\/dd hh\:nn\:ss\#")
            End If
        End If
    End Function
    Yes it interprets your date in vba correctly, but when you pass it to the SQL engine it doesn't know about your regional settings so has to make an assumption, it always assumes it's a US date, unless it knows it can't be e.g. 12/04/2018, SQL will think it is Dec 4th , however 13/04/2018 will be interpreted as April 13th, as it couldn't be anything else.
    Damn confusing isn't it ...
    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 ↓↓

  9. #9
    Samson is offline Novice
    Windows 10 Access 2019
    Join Date
    Jun 2023
    Location
    Bristol, England
    Posts
    7

    Possible solution for UK to US date format flipping in SQL

    I've been at this for a couple of days now, but I think I may have solved the problem of writing dates back to the database when using UK formatting and the date changing to US format.

    Once you have created your new date in UK format, before writing it to the database convert it to US format and use that format in the SQL statement, like this:

    Code:
    Private Sub btnUpdate_Click()
        Dim dteCurrentDate As Date, dteNewDate As Date, dteUSDate As Date
    
        'Get current date
            dteCurrentDate = DLookup("CurrentDate", "tblDate", "ID=1")
    
            MsgBox "Current Date: " & dteCurrentDate
    
        'Calculate new date
            dteNewDate = DateAdd("ww", 1, dteCurrentDate)
    
            MsgBox "New Date: " & dteNewDate
    
        'Convert date to US format for SQL statement
            dteUSDate = Format(dteNewDate, "mm/dd/yyyy")
    
            MsgBox "US Date: " & dteUSDate
    
        'Update current date
            DoCmd.RunSQL ("UPDATE tblDate SET CurrentDate = #" & dteUSDate & "# WHERE ID = 1")
    
        'Refresh form
            Me.Refresh
    
    End Sub
    If you now have a date that could be either US or UK format and you region is UK, the date is written in US format but displayed in UK format.

    Hope this helps anybody else that has been banging their head on a wall for days!
    Last edited by Samson; 10-15-2023 at 01:22 AM.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Only that last variable is a Date type, the rest are Variants?
    You must explicitly define each one.
    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

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Found this years ago when I was using Access in the office. Place in a general module.

    Public Const strcJetDate = "\#mm\/dd\/yyyy\#" 'Needed for dates in queries as Access expects USA format.

    Just use as Format(YourDate, strcJetDate) in any SQL string
    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

  12. #12
    Samson is offline Novice
    Windows 10 Access 2019
    Join Date
    Jun 2023
    Location
    Bristol, England
    Posts
    7
    Quote Originally Posted by Welshgasman View Post
    Only that last variable is a Date type, the rest are Variants?
    You must explicitly define each one.
    Sorry, I don't understand what you mean.
    The date variables are defined in the Dim statement at the top of the code.
    Is that what you mean? I'm confused!

  13. #13
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    Perhaps a bit of reading would be beneficial?

    https://learn.microsoft.com/en-us/of...ring-variables

  14. #14
    Samson is offline Novice
    Windows 10 Access 2019
    Join Date
    Jun 2023
    Location
    Bristol, England
    Posts
    7
    Ah! You learn something new every day. Thanks.

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

Similar Threads

  1. Replies: 7
    Last Post: 12-03-2015, 08:37 PM
  2. Replies: 6
    Last Post: 08-27-2015, 01:06 PM
  3. Replies: 3
    Last Post: 05-19-2015, 03:04 PM
  4. Group by pulling incorrect results
    By jpawson74 in forum Queries
    Replies: 7
    Last Post: 03-28-2012, 09:46 AM
  5. Access Min Max Comparison Yielding Incorrect Results
    By MetalCheese in forum Programming
    Replies: 6
    Last Post: 12-06-2011, 04:29 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