I'll try to explain as clearly and simply as I can.
Dates are stored the same way regardless. This is not about how they're stored. It's about how they're displayed. The problem is that people use the default display format in order to build up SQL strings (or commands) in their code.
If you say
strX = datVar
then the VBA interpreter will see a string on one side and a
Date on the other and will know the
Date needs to be converted to a string, so it works out which characters to include in this sub-string using what it knows about your locale settings. In the USA it will select a format that is actually validly recognised by SQL as exactly the date you're thinking of. In much of the rest of the world it would not.
A SQL command string is not designed for human viewing. That's an important point. It's
not designed for human viewing. SQL is a command interpreter and it does so following clearly defined and specific rules. Just dumping some digits and other stuff that look to a human like it may be a date is absolutely
not good enough. It must follow a standard format otherwise it's garbage.
You'll understand that exactly the same is going on when you say :
- strX = "... WHERE ([DateField] Between #" & datVar & "# And Date())"
It sees a
Date value that needs to be converted to a string. It uses your locale settings. Its only concern is to produce characters in order that represent a date string to the local human populace. There is nothing there to ensure it matches SQL date requirements. In most of the world it won't.
Consider one of the worst case scenarios. It's the first day of August and you're running the application in the United Kingdom instead of USA. Instead of :
- ... WHERE ([DateField] Between #8/1/2018# And Date())
You get :
- ... WHERE ([DateField] Between #1/8/2018# And Date())
Why is that a worst-case scenario? Because it
doesn't fail! Users are likely to think all is well yet it's just used a completely different date from the one intended. Your business figures have just gown down the pan because someone sloppily referred to 8th January instead of 1st August.
With :
- ... WHERE ([DateField] Between #2018-8-1# And Date())
That can never happen of course.