Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Can some explain this in dates?

    I've read dozens of blogs on dates and walk away each time more confused than ever.
    This may be a numbnut question, but I keep coming back to this and am baffled.

    I'm using the Colombia region settings, so that means d/mm/yyy.

    If I do this in the immediate window:



    Code:
    ?now
    14/09/2023 10:38:45 p. m. 
    
    
    ?#1-2-3#
    2/01/2003 
    
    
    ?cdate("1/2/3")
    
    1/02/2003
    Do we use the "#" in VBA for other than building SQL statements?
    Why did the second example flip the date and month (if it's not only for SQL in a USA format)?

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    By default Access manipulates dates in US format mm/dd/yyyy.
    As you can imagine this is very confusing for any other locale.

    This is where it gets silly - Any Date presented between # # delimiters are assumed to be in mm/dd/yy format, UNLESS it can't be e.g. #23/11/23#
    In this case Access "helpfully" understands that it must be the 23-Nov-2023. You can see this effect in the query builder and the immediate window.

    The best way to circumvent this behaviour is to create a function that forces all dates into a unambiguous format when you are building SQL statements- and I prefer #yyyy-mm-dd# which works in every locale and on SQL server if you ever go there.
    This is a variant of the Allen Browne function:

    Code:
    Function ServerDate(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
                ServerDate = Format$(varDate, "'yyyy-mm-dd'")
            Else
                ServerDate = Format$(varDate, "'yyyy-mm-dd hh:nn:ss'")
            End If
        End If
    End Function
    The other benefit of this format is that during debugging, there is no misunderstanding or wondering what format the date being passed around is in.
    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
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    # tells sql that the string between them is to be treated as a date, it works in vba as well. In the case of an ambiguous string, it defaults to the US format/value of mm/dd/yyyy.

    so this in unambiguous because there are not 15 months in the year
    ?#15-09-2023#
    15/09/2023

    this is ambiguous, so defaults to US format
    ?#10-09-2023#
    09/10/2023

    In VBA you can use the # in a calculation although personally, I wouldn't



    ?#15-09-2023# = date()
    True

    but only works if the string is unambiguous - so this calc fails (which is why I wouldn't)
    ?#10-09-2023# = (date()-5)
    False

    So your example of #1-2-3# is ambiguous (but see below for another issue)

    cDate on the other hand returns the date value in the format per your windows settings

    ?cdate("10-09-2023") = (date()-5)
    True

    be aware that your test data is not really valid for using #. For example an 'unambiguous' day of say 15 treats the 15 as being the year (per sql standard of yyyy-mm-dd





    ?#15-1-3#
    03/01/2015

    whereas with cdate

    ?cdate("15/1/3")
    15/01/2003

    Edit - I'm with Minty - I always format to yyyy-mm-dd if I need to use # in sql

  4. #4
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @ Minty & CJ_London Thanks, and I saw most of this in the blogs of Richard Rost, Mike Wolfe and Philipp Stiefel. I decided on the ISO format long ago, displaying in the German format (which has me going nuts in testing when entering in this format) and I'm trying to work theirs and something similar to Minty's into my own set of routines that pass back a global error for logic checking. My head started spinning when consolidating all the blogs, considering, entry vs. query vs. VBA, Nulls and now ZLS in dates, which no one seems to consider. Then there is the user that may refuse to enter a date in an ISO format, and insists on using their regional format which then may confuse my logic as well as Access. The permutations had me going bonkers especially after 12 hours of this and writing functions. Add to that, I was modifying all my code to accommodate Mike Wolfe's DocTests function that needs modification for my global err idea. Aggghhhh!

    For example, the modified Allen Browne code ignores Nulls and ZLS, while the Mike and Phillipp solutions ignore ZLS.

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Then there is the user that may refuse to enter a date in an ISO format, and insists on using their regional format which then may confuse my logic as well as Access.
    You are aware that dates are stored as doubles, all the different views of a date is just a format

    ?cdbl(date())
    45184
    ?cdbl(now())
    45184.4956597222

    the folowing works because the date is not ambiguous
    ?cdbl(#15/9/2023#)
    45184
    ?cdbl(#09/15/2023#)
    45184

    this doesn't for dd/mm formats because it is ambiguous
    ?cdbl(#12/9/2023#)
    45269

    but these do
    ?cdbl(cdate("12/9/2023"))
    45181

    ?cdbl(cdate("2023-09-12"))
    45181

    ?cdbl(cdate("12/9/23"))
    45181



    ?cdbl(cdate("12-9-23"))
    45181




    so it should not matter what format they enter a date in. It has to be a valid format for the region (I would suggest that '1/2/3' is not valid) and in the appropriate date range which is standard validation. If you want to allow users to be sloppy in not using a proper format, that is down to you.

    Only time I can see it being an issue is if you have users in different time zones and the date needs to be consistent to one time zone - in which case you would need to adjust the entered date by adding/subtracting the appropriate number of hours to get to a single time zone (seem to recall you had a question on that some time ago). Or if all users are are on the same server, use the server time.

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    By default, Access will recognise the local regional date format if it is input on a form provided the field is bound to a date.

    So in the UK 2/3/23 on a form or even 2/3 is correctly interpreted, and stored as 02-March-2023 (Or more accurately is stored as 44987.00)


    It is only when taking that date/ form value and doing something in VBA that you need to be aware of how it is handled.
    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
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    @ CJ_London & Minty, posts #5 & 6. Yes, I know how dates are stored in Access, that isn't the problem. And yes, I have multiple servers, each with a time offset from the main server. Users can connect to different servers via Terminal from different time-zones using different regional settings. I've burned out a lot of brain cells on that problem.

    If access allows "1/2/3" then who am I to say (to a user with their particular region and manner of working) it's not valid? Confusing yes, but I have to deal with that confusion. In addition, I have to get users to think in terms of a total time, not just the date. Some blogs on doing proper Between dates (not using "Between") abound, but that's more a developer problem than a user issue.

    For now, the brain numbing has been caused by the "#", where it's properly used, Nulls and ZLS and what VBA and ACE need. I keep getting hints that it gets more complicated with SQL server, and I'm not sure if it gets even more complicated using other than MS SQL Server, like Azure.

    I am working mostly in VBA now, where one isn't likely to hard code a date. In queries though, dates are often "hard coded".

    Thanks again for your ideas, it helps me to sort out all the confusing concepts to see how others deal with them.

  8. #8
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    As far as I'm aware a bound date control can't accept as ZLS.
    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
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Code:
    who am I to say (to a user with their particular region and manner of working) it's not valid?
    you're the developer, that's who. If your client states that's what they want, then you need clarity on all the different ways a user is allowed to enter the data. Who's to say 1/2/3 means 2003 and not 1903 or 2013 or 2023? or any other year that ends in a 3. Perhaps look at some of the other apps these users are using, might give you a clue.

    This is not an access problem, it will apply to any language - perhaps you can point to a website where a user can enter 1/2/3 and it produces the correct date, whatever that may be.

    Good luck, but without a clear definition of what 1/2/3 means, there is nothing you can do and nothing more I can add

  10. #10
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Nulls, ZLS and dates

    Let me take a moment to share what I learned from my experimenting in query builder.
    I created a simple table. In ID 3 I just tabbed through the entry. In ID 4 I entered "" for a ZLS, thinking that I might want to say "The date doesn't exist" rather than "The date is unknown". I wasn't sure how Access was going to deal with this.

    Here's the query design:

    Attachment 50763

    And the result:

    Attachment 50764

    Adding to the confusion is that Access allows a ZLS entry, but apparently changes it to a Null.

    Mike Wolfe offered this code for dates:

    Code:
    Function Dt(DateVal As Variant) As StringConst Pound As String = "#"
        If IsNull(DateVal) Or IsEmpty(DateVal) Then
            Dt = "Null"
        Else
            Dt = Pound & DateVal & Pound
        End If
    End Function
    but I'm not sure how "Null" is going to work in a query, because the above test needs "Is Null", but since Mike didn't include an example using Null, I'm not sure I understand what he was doing.

  11. #11
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    neither attachments are attached - on this forum you can't copy/paste.

    you can try

    if nz(dateval,"")="" then

  12. #12
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    Quote Originally Posted by CJ_London View Post
    neither attachments are attached - on this forum you can't copy/paste.
    I used "Insert Image" from the toolbar. Is this the wrong way to add an image?

    Here, I'll try again the same way:

    Click image for larger version. 

Name:	230915Dates1.jpg 
Views:	11 
Size:	57.0 KB 
ID:	50765

    Click image for larger version. 

Name:	230915Dates2.jpg 
Views:	11 
Size:	42.6 KB 
ID:	50766

    Before saving, it looks good in what I'm seeing.

    When I look at post #10, I can see links for the attachments, and they work. I don't always get the same result when posting images, and I'm not sure why.

  13. #13
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    As I said you can't store a ZLS in a date field, it will simply be a Null
    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 ↓↓

  14. #14
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    What's wrong with the function?

    I'm testing my Mike inspired function, called fDt, and it returns this:

    Code:
    ?fdt("2003-2-1")
    #2003-02-01 00:00:00#
    Yet, when I use it in a query criterion, it errors out:

    Click image for larger version. 

Name:	230915Dates3.jpg 
Views:	12 
Size:	63.9 KB 
ID:	50767

    After note: As best as I can figure out, Mike returns a string. So, I'm guessing the function can't be used straight-up in a query, that he meant it only to be used in building VBA SQL strings. I'm guessing that it's also true for his other quoting and date between functions. That makes testing the functions in a query a bit more difficult if not impossible.

  15. #15
    twgonder is offline Expert
    Windows 10 Access 2021
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    Quote Originally Posted by Minty View Post
    As far as I'm aware a bound date control can't accept as ZLS.
    It accepted it (in a table entry) without a problem. What it does with it is another issue.
    I'll go test a form.

    After note: Entered "" just fine in a form. Windows 11 with Access 2021.

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

Similar Threads

  1. Why is it so hard to explain VBA?
    By DigitalAdrenaline in forum Access
    Replies: 1
    Last Post: 08-20-2016, 06:04 AM
  2. Hard to explain how to do this?
    By flemingjo in forum Forms
    Replies: 1
    Last Post: 08-29-2014, 06:03 AM
  3. Can someone explain what this means?
    By Jay13 in forum Access
    Replies: 5
    Last Post: 05-23-2013, 04:22 PM
  4. Explain iif in ms access
    By pabbiraj in forum Programming
    Replies: 2
    Last Post: 05-27-2012, 01:20 AM
  5. Could someone please explain to me what a Switchboard is
    By wabbalee in forum Database Design
    Replies: 2
    Last Post: 06-26-2009, 07:20 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