下面的代码是根据我在我从事的几乎每个项目(在 Access 中)中使用的一些样板文件稍加修改的。此代码的所有其他版本都可以在此数据库中完美运行。这些表实际上都在 SQL Server 中,但它们在此 Access db 中链接。
这是奇怪的部分......当我运行代码并进入“With rs”并执行“.Open”时,下一步“Do While Not .EOF”会跳到“Do”的底部。 .我在.EOF!但是,如果我获得“.Source”SQL 语句的副本并将其粘贴到非代码 Access 主窗口上的空白 SQL 查询中,我会从该查询中获得 835 条记录。当这个查询应该返回 835 条记录时,我怎么能得到一个空的记录集?
我尝试更改我的 .CursorType 和 .LockType,但无济于事。
有人对我可以尝试解决此问题有什么想法吗?
Public Sub test()
Dim db As ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sFromDate As String
Dim sToDate As String
Dim sSubUnit As String
sFromDate = "1/1/2013"
sToDate = "1/31/2013"
sSubUnit = "2???"
' Make a connection to the db
Set db = CurrentProject.Connection
With rs
.ActiveConnection = db
.CursorType = adOpenForwardOnly
.LockType = adLockReadOnly
.Source = "SELECT SCEVENT.CLIENT_ID, SCATTEND.EMP_ID, SCATTEND.STARTDATE, SCATTEND.SVC_ID " & _
"FROM SCATTEND LEFT JOIN SCEVENT ON SCATTEND.SCEVENT_ID = SCEVENT.ID " & _
"WHERE (SCATTEND.STARTDATE Between #" & sFromDate & "# And #" & sToDate & "#) " & _
"AND (SCATTEND.SUBUNIT_ID Like '" & sSubUnit & "') " & _
"AND ((APPTYP_ID IS NULL) OR (APPTYP_ID IN (1,2))) " & _
"ORDER BY SCEVENT.CLIENT_ID, SCATTEND.EMP_ID, SCATTEND.STARTDATE, SCATTEND.SVC_ID;"
.Open
Do While Not .EOF
MsgBox "there are records!"
.MoveNext
Loop
.Close
End With
Set rs = Nothing
Set db = Nothing
结束子