问题
My Access database displays dates in DD/MM/YYYY
format. One row contains date as 07/06/2014
, but my query says "not found".
Here is my select query:
strSql = "SELECT * FROM Tbl WHERE MyDate = #" & Me.fldFindWhat.Value & "#"
MsgBox strSql
displays
SELECT * FROM Tbl WHERE MyDate = #07/06/2014#
Where is the problem then?
回答1:
This is a common issue with Access date queries for people who use the dd/mm/yyyy
format. The problem is that the Access Database Engine does not pay any attention to the Regional Settings in Windows itself; it always interprets ambiguous #xx/yy/zzzz#
date literals as mm/dd/yyyy
. Adding to the confusion is the fact that it will interpret unambiguous #xx/yy/zzzz#
date literals as dd/mm/yyyy
if xx
is greater than 12.
So, the Access Database Engine will interpret #13/06/2014#
as June 13, 2014
. The date literal is unambiguous since there is no 13th month.
However, the Access Database Engine will always interpret #07/06/2014#
as July 6, 2014
. This is true regardless of the Regional Settings in the Windows Control Panel.
To avoid this problem, always use the unambiguous yyyy/mm/dd
format in date literals, e.g.,
strSql = "SELECT * FROM Tbl WHERE MyDate = #" & _
Format(Me.fldFindWhat.Value, "yyyy\/mm\/dd") & "#"
You could also use the mm/dd/yyyy
format in date literals, e.g.,
strSql = "SELECT * FROM Tbl WHERE MyDate = #" & _
Format(Me.fldFindWhat.Value, "mm\/dd\/yyyy") & "#"
... but people who are used to seeing dd/mm/yyyy
dates could find this confusing when analyzing debug output.
来源:https://stackoverflow.com/questions/25698297/access-date-query-using-dd-mm-yyyy-returns-zero-found