Access date query using dd/mm/yyyy returns zero found

妖精的绣舞 提交于 2019-12-31 02:45:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!