通过 VBA 将日期/时间与 Access DB 中的日期/时间进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1941869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
compare date/time via VBA with date/time in Access DB
提问by user236248
How to compare a date/time via VBA with a date/time in an Access DB?
如何通过 VBA 将日期/时间与 Access DB 中的日期/时间进行比较?
The query I use
我使用的查询
adoRS.Open "SELECT * FROM currentpositions WHERE ((currentpositions.
[dateLT])=" & "#" & date_from_message & "#" & ")", adoConn, adOpenStatic,
adLockOptimistic
I only achieve to compare a date.
Anybody an idea?
我只实现比较日期。
有人有想法吗?
Regards
Camastanta
问候
卡马斯坦塔
回答by shahkalpesh
adoRS.Open "SELECT * FROM currentpositions WHERE DateValue(currentpositions.
[dateLT]) = DateValue(" & "#" & date_from_message & "#)", adoConn, adOpenStatic,
adLockOptimistic
See, if the above helps.DateValue
extracts the date part from a given date/time. So, it can be used to compare date, ignoring the time part.
看看,如果以上有帮助。DateValue
从给定的日期/时间中提取日期部分。因此,它可用于比较日期,而忽略时间部分。
回答by Jakob Christensen
IMHO you should never build your SQL statements using string concatenation. Instead use parameterized SQL queries. This will save you from problems like the one you are facing with date/time comparison.
恕我直言,您永远不应该使用字符串连接来构建您的 SQL 语句。而是使用参数化 SQL 查询。这将使您免于遇到诸如日期/时间比较之类的问题。
回答by Tony Toews
Does your system have dates in either yyyy-mm-dd or dd-mm-yyyy format? If so see Return Dates in US #mm/dd/yyyy# formatto convert your dates in mm-dd-yyyy so Access can work with them properly.
您的系统是否有 yyyy-mm-dd 或 dd-mm-yyyy 格式的日期?如果是这样,请参阅美国 #mm/dd/yyyy# 格式的返回日期以将日期转换为 mm-dd-yyyy,以便 Access 可以正常使用它们。