如何正确设置 DateTime 值的 ADODB.Recordset.Filter 属性?

发布于 2024-12-28 18:01:59 字数 366 浏览 0 评论 0原文

我有一个 ADODB 2.x 记录集,并尝试将过滤器应用于记录集中的日期时间列/字段。

我不知道如何在 Windows 中配置国际日期格式设置。通过大量谷歌搜索,我不完全确定 Windows 日期格式设置是否确定了 Filter 属性所需的日期格式,或者是否始终采用美国日期格式。 (当然,MS文档没有注意到这

一点。)那么,如何正确编写这一行代码,这样我就不必担心用户在Windows中配置了什么日期格式:

rs.Filter = "StartDateTime >= " & dteStartDate

...where dteStartDate 包含日期和时间组件。

I have an ADODB 2.x recordset and am trying to apply a filter to a DateTime column/field in the recordset.

I do not know how the has configured their international date format settings in Windows. From lots of googling, I am not entirely sure if the windows date format settings determines the required date format for the Filter property, or whether it is always expected in US date format. (Of course, the MS documentation makes no note of this.)

So, how does one properly write this line of code, so I don't have to worry what date format the user has configured in Windows:

rs.Filter = "StartDateTime >= " & dteStartDate

...where dteStartDate contains both a Date and Time component.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与往事干杯 2025-01-04 18:01:59

过滤器的格式需要与记录集中的值格式相同。我刚刚在 MS SQL 服务器的记录集上遇到了同样的问题。记录集包含 YYYY-MM-DD 格式的日期。

失败:

CalendarRS.filter = "DateField = #" & datevalue(DisplayDate) & "#"

即使日期匹配,筛选的记录集中也没有记录。选中时,CalendarRS.filter 中存储的值是“DateField = #08/29/2014#” 请注意,这包含正斜杠并且顺序不同 (MM/DD/YYYY)

成功:

CalendarRS.filter = "DateField = #" & year(datevalue(DisplayDate)) & "-" &  right("00" & month(datevalue(DisplayDate)),2) & "-" &  right("00" & day(datevalue(DisplayDate)),2) & "#"

选中时,存储的值在 CalendarRS.filter 中为“DateField = #2014-08-2014#”。这返回了正确的过滤记录集。

对于完整的日期和时间,您需要执行相同的操作并添加小时、分钟、秒。

 CalendarRS.filter = "DateField = #" & year(datevalue(DisplayDate)) & "-" & right("00" & month(datevalue(DisplayDate)),2) & "-" & right("00" & day(datevalue(DisplayDate)),2) & " " & Right("00" & Hour(datevalue(DisplayDate)),2) & ":" & Right("00" & Minute(datevalue(DisplayDate)),2) & ":" & Right("00" & Second(datevalue(DisplayDate)),2) & "#"

The format of the filter needs to be in identical format as the value in the recordset. I just ran into the same issue with a recordset from a MS SQL server. the recordset contained a date in YYYY-MM-DD format.

This failed:

CalendarRS.filter = "DateField = #" & datevalue(DisplayDate) & "#"

Even if the date matched, no records were in the filtered recordset. When checked, the value stored in CalendarRS.filter was "DateField = #08/29/2014#" Note that this contains forward slashes and that the order is different (MM/DD/YYYY)

This succeeded:

CalendarRS.filter = "DateField = #" & year(datevalue(DisplayDate)) & "-" &  right("00" & month(datevalue(DisplayDate)),2) & "-" &  right("00" & day(datevalue(DisplayDate)),2) & "#"

When checked, the value stored in CalendarRS.filter was "DateField = #2014-08-2014#". This returned the correct filtered recordset.

For a full date and time, you need to do the same thing and add hour, minute, second.

 CalendarRS.filter = "DateField = #" & year(datevalue(DisplayDate)) & "-" & right("00" & month(datevalue(DisplayDate)),2) & "-" & right("00" & day(datevalue(DisplayDate)),2) & " " & Right("00" & Hour(datevalue(DisplayDate)),2) & ":" & Right("00" & Minute(datevalue(DisplayDate)),2) & ":" & Right("00" & Second(datevalue(DisplayDate)),2) & "#"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文