实际上,我对WHERE Format([TimeStamp]...
在针对 Access 数据库引擎 (ACE) 的 ADO 查询中起作用感到非常惊讶,但显然确实如此。
我当然同意在 Microsoft 的文档中很难找到有关使用某些 Microsoft 功能的具体细节。我想这有助于保持 Stack Overflow 等网站的业务。;)
至于您的.Filter
问题,Format()
在这种情况下使用确实会失败,大概是因为Format()
它是一个 VBA 函数,并且不能(总是)可用于 Access 应用程序本身之外的表达式。但是,以下测试表明...
rst.Filter = "[TimeStamp] >= #2013/04/11# AND [TimeStamp]<#2013/04/12#"
...确实有效。(如果没有为 DateTime 值指定时间,00:00:00
则假定为午夜 - -。)
测试数据:
ID TimeStamp
1 2013-04-10 21:22:00
2 2013-04-11 02:34:56
3 2013-04-11 04:45:15
测试代码:
Sub foo()
Dim con As ADODB.Connection, rst As ADODB.Recordset
Set con = New ADODB.Connection
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data source=C:\Users\Gord\Desktop\Database1.accdb;"
Set rst = New ADODB.Recordset
Debug.Print "Test 1: WHERE Format([TimeStamp]..."
rst.Open _
"SELECT * FROM [TimeStampData] " & _
"WHERE Format([TimeStamp], ""yyyy/mm/dd"") = Format(#2013/04/11#, ""yyyy/mm/dd"")", _
con, adOpenKeyset, adLockOptimistic
Debug.Print "Records returned: " & rst.RecordCount
rst.Close
Debug.Print
Debug.Print "Test 2: Filter"
rst.Open "SELECT * FROM [TimeStampData]", con, adOpenKeyset, adLockOptimistic
Debug.Print "Total records: " & rst.RecordCount
rst.Filter = "[TimeStamp] >= #2013/04/11# AND [TimeStamp]<#2013/04/12#"
Debug.Print "Filtered records: " & rst.RecordCount
rst.Close
Set rst = Nothing
con.Close
Set con = Nothing
End Sub
测试结果:
Test 1: WHERE Format([TimeStamp]...
Records returned: 2
Test 2: Filter
Total records: 3
Filtered records: 2