1

I'm having problems with date format in an SQL statement

CurrentTime = Format(CurrentTime, "dd/mm/yyyy hh:mm")
SQL = "SELECT Count([ID]) AS Booked FROM [Appointment Slots] WHERE [Appointment Slots].Time=#" & CurrentTime & "#"

The bizarre thing here is that sometimes when I run the code this works. Other times, and without changing the code, it doesn't, but then it works when I change the date format to mm/dd/yyyy hh:mm then it works for a while then stops working until I change it back to dd/mm/yyyy hh:mm

Clearly something is going wrong with the regional date settings and how it's storing dates but I can't pin down a solution to this. Is there no way to compare a date in Access SQL that is independent of format?

2
  • A parameter query is a neat way to avoid date format issues, and also avoid needing to include # delimiters for date literals. If the query expects a date parameter, you just give it a date value. Commented Mar 10, 2016 at 15:11
  • This worked best of the solutions I tried, can you write it as an answer so I can mark it as accepted? Commented Mar 11, 2016 at 10:43

3 Answers 3

4

You should make it a habit using the ISO sequence, and nn for minutes.

Also escape the "/" and ":" to have a true slash and colon, otherwise they will be replaced with the localized date and time separators:

CurrentTime = Format(CurrentTime, "yyyy\/mm\/dd hh\:nn")

This also works for ADO and FindFirst, which the "reverse" US format (mm/dd/yyyy) does not.

Sign up to request clarification or add additional context in comments.

5 Comments

"otherwise they will be replaced with the localized date and time separators" Thanks for that. Would "yyyy-mm-dd" format also be subject to that localization substitution?
No, and indeed not for dates where / and : are the only separator characters. Elsewhere, you also have the dot as the separator for milliseconds, but that is not handled natively by Access and VBA.
Thanks again. I'd seen "yyyy\/mm\/dd" used before, but never understood why it was useful. Since I'm in U.S., I'm too ignorant about many localization details. ;-)
I think it is in France they use a space as a separator. That can really cause strange happenings in SQL.
Thanks, this still didn't work however. Resetting my regional settings to US formats and then re-entering data in US format and the code worked. Also - the suggestion from HansUp in a comment on the OP of using parameter queries also seemed to work.
1

Is there no way to compare a date in Access SQL that is independent of format?

Consider a query with a parameter in its WHERE clause, similar to this ...

WHERE [Appointment Slots].Time=[Enter Appointment Time]

You can also add a PARAMETERS clause at the beginning of your SQL statement, but it's not absolutely required ...

PARAMETERS [Enter Appointment Time] DateTime;

So when the query expects a Date/Time parameter, you just give it a Date/Time value.

A parameter query avoids date format issues, and also avoids the need for # date delimiters in the SQL statement.

Comments

1

Date format #....#, used in queries works correctly only with american date format mm/dd/yyyy

The best way for working with dates is passing them as parameters to query. In this case used internal format, which doesn't depend on regional settings.

1 Comment

It also works with the ISO sequence. And using a slash only will cause the expression to be localized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.