Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    ano is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    59

    confusing date handeling

    vba seems only to understand USA standard even all other MS support non USA standard
    dim a,b as date
    a=#21/12/1999#
    -->
    a = #12/21/1999#
    b=#1/3/1999#
    -->
    b = #1/3/1999#
    seems its already known many years
    but i not really found solution for this


    no zip file u need to type in vba ur self to see it happen

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Use yyyy-mm-dd format. That cannot be interpreted any other way.
    When you have 21/12/1999 Access knows that there are not 21 months in the year so it must be in ddmmyyyy format. When you have 01/03/1999 it cannot tell. so assumes mmddyyyy format as that is what it expects.

    Just get used to it.

    ? #2024-01-01#
    01/01/2024
    ? #2024-12-31#
    31/12/2024
    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

  4. #4
    ano is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    59

    no not really , its common use

    Quote Originally Posted by pbaldy View Post
    tnx but no tnx . it not fit my filter problem, at least i cannot find/understand the article describe how to filter

    Forms!form1.advance_subform.Form.filter = "(format([datum],'yyyy/mm/dd HH:mm:ss') between format(#" & v6 & "#,'yyyy/mm/dd HH:mm:ss') and format(#" & v5 & "#,'yyyy/mm/dd HH:mm:ss'))"
    Forms!form1.advance_subform.Form.FilterOn = True

    what i think i miss is an option to tell "dd/mm/yyyy" to "yyyy/mm/dd"
    i guess underwater the input date is always mm/dd/yyyy unless the month not exist
    manipulating with database dates result mostly result in problems

    this is even more conusing: Misinterpretation in the User Interface

    in office application u have to set ur date or accept system date, so how can date get error if the office application is leading?

    enjoy the new year

  5. #5
    ano is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    59
    thnx for ur advice , but with few years dd/mm/yyyy its hard to change this into yyyy/mm/dd
    i know bec i tried before and stopped looking at the dates problem long time it is known but no solution.

    thnx enjoy the new year

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    i guess underwater the input date is always mm/dd/yyyy unless the month not exist
    No, I already told you in other threads.

    Dates are stored as numbers. Before the decimal point is the date, after the decimal point is the time.
    How you see it/use it depends on your regional settings.
    However if you need to use it as a string, then it has to be either yyyy-mm-dd or mm/dd/yyyy. The latter can be ambiguous, the former cannot.

    There are rules in programming. That is just one for Access and Excel VBA.
    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

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by ano View Post
    tnx but no tnx . it not fit my filter problem, at least i cannot find/understand the article describe how to filter

    Forms!form1.advance_subform.Form.filter = "(format([datum],'yyyy/mm/dd HH:mm:ss') between format(#" & v6 & "#,'yyyy/mm/dd HH:mm:ss') and format(#" & v5 & "#,'yyyy/mm/dd HH:mm:ss'))"
    Forms!form1.advance_subform.Form.FilterOn = True

    what i think i miss is an option to tell "dd/mm/yyyy" to "yyyy/mm/dd"
    i guess underwater the input date is always mm/dd/yyyy unless the month not exist
    manipulating with database dates result mostly result in problems

    this is even more conusing: Misinterpretation in the User Interface

    in office application u have to set ur date or accept system date, so how can date get error if the office application is leading?

    enjoy the new year
    Put your filter into a string variable and then you can debug.print it and get it correct.
    Once correct, you can use that in the filter.

    Much much easier.
    Same applies to 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

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    You can also use a constant for the formatting.

    Code:
    Const JetDte As String = "\#mm\/dd\/yyyy\#"
    Const JetDte2 As String = "\#mm\/dd\/yyyy hh\:nn\:ss\#"
    Code:
    ?Format("13/1/2024",JetDte)
    #01/13/2023#
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by moke123 View Post
    You can also use a constant for the formatting.

    Code:
    Const JetDte As String = "\#mm\/dd\/yyyy\#"
    Const JetDte2 As String = "\#mm\/dd\/yyyy hh\:nn\:ss\#"

    Code:
    ?Format("13/1/2024",JetDte)
    #01/13/2023#
    Now that is clever. It even changes the year?
    Code:
    ? cdbl(date)
     45293
    ? format(45293,"\#mm\/dd\/yyyy\#")
    #01/02/2024#
    I am in the UK.
    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
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Quote Originally Posted by moke123 View Post
    You can also use a constant for the formatting.

    Code:
    Const JetDte As String = "\#mm\/dd\/yyyy\#"
    Const JetDte2 As String = "\#mm\/dd\/yyyy hh\:nn\:ss\#"
    Code:
    ?Format("13/1/2024",JetDte)
    #01/13/2023#
    Nice. Instead of constant you can also use a function:

    Code:
    '---------------------------------------------------------------------------------------
    ' Method : fcnGetDelimDate
    ' Author : davegri & moke123
    ' Date   : 01/02/24
    ' Purpose: Returns delimited(#) date as string
    '   arg can be such as
    '   "2/2/22"
    '   "2/2/2022"
    '   dateadd("m",1,"2/2/22")
    '   "41234"
    '   41234
    '   Date()
    '
    '   arg cannot be
    '   2/2/22 or 2/2/2022  ( resolves as #12/30/1899# after the division arithmetic completes )
    '---------------------------------------------------------------------------------------
    Public Function fcnGetDelimDate(arg As String) As String
        Dim JetDte As String: JetDte = "\#mm\/dd\/yyyy\#"
        Dim JetDte2 As String: JetDte2 = "\#mm\/dd\/yyyy hh\:nn\:ss\#"
        fcnGetDelimDate = Format(arg, JetDte)
    End Function

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    How does that get to use JetDte2? if a date and time is passed?
    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
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Quote Originally Posted by Welshgasman View Post
    How does that get to use JetDte2? if a date and time is passed?
    Make another function that uses JetDte2

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    How about

    Code:
    '---------------------------------------------------------------------------------------
    ' Method : fcnGetDelimDate
    ' Author : davegri & moke123
    ' Date   : 01/02/24
    ' Purpose: Returns delimited(#) date as string
    '   arg can be such as
    '   "2/2/22"
    '   "2/2/2022"
    '   dateadd("m",1,"2/2/22")
    '   "41234"
    '   41234
    '   Date()
    '
    '   arg cannot be
    '   2/2/22 or 2/2/2022  ( resolves as #12/30/1899# after the division arithmetic completes )
    '---------------------------------------------------------------------------------------
    Public Function fcnGetDelimDate(arg As String) As String
        Dim JetDte As String: JetDte = "\#mm\/dd\/yyyy\#"
        Dim JetDte2 As String: JetDte2 = "\#mm\/dd\/yyyy hh\:nn\:ss\#"
        If InStr(arg, ":") = 0 Then
            fcnGetDelimDate = Format(arg, JetDte)
        Else
            fcnGetDelimDate = Format(arg, JetDte2)
        End If
    End Function

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I seem to be missing the point here?
    If the date is ambiguous, then you can still have a bad date?

    Most times I would be passing an actual date, that I needed to be formatted as a string for concatenation for a filter or 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

  15. #15
    Join Date
    Apr 2017
    Posts
    1,679
    As Welshgasman stated in post #6, dates are really numbers. As follows, when you cave to cope with dates from different local settings, you will skip all this formatting nonsense, when you handle those dates as numbers.

    Examples:
    You have a table MyTable with a field MyDate formatted as date (in whatever format you want);
    When running a query <INSERT INTO ByTable (MyDate) VALUES (10000)>, a date for May 18th of year 1927 is inserted (in format you used);
    When after that run a query <SELECT MyDate FROM MyTable WHERE MyDate = 10000>, the rows of MyTable with MyDate = May 18th of year 1927 are displayed;
    When run a query <SELECT 1*MyDate FROM MyTable>, numeric values of all dates in MyTable are displayed;
    Etc.

    The only case when this doesn't work directly is, when your Access doesn't recognize one of the read dates as a date. When this is the case, I usually read numeric values for year, month, and day for such dates, and compose a date the Access recognizes as such.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Handeling multiple languages
    By sisva18 in forum Programming
    Replies: 3
    Last Post: 08-26-2021, 12:53 PM
  2. Handeling If null
    By Homegrownandy in forum Queries
    Replies: 14
    Last Post: 01-10-2017, 09:43 AM
  3. Query handeling empty FK keys
    By warlock916 in forum Access
    Replies: 1
    Last Post: 01-12-2016, 07:18 AM
  4. This might be confusing
    By djclntn in forum Queries
    Replies: 7
    Last Post: 12-08-2011, 10:56 PM
  5. Handeling Multipule Units of Measure
    By Waldenbound in forum Database Design
    Replies: 1
    Last Post: 09-02-2011, 08:24 AM

Tags for this Thread

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