运行时错误3021 - EOF或BOF为真或当前记录已被删除

时间:2013-12-11 23:41:47

标签: vba ms-access access-vba

rst.Open "SELECT * FROM Equipas WHERE ([ID - Funcionário] LIKE '" & idfunc & "' AND [ID - Tarefa] LIKE ' " & idtask & "' );", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
rst.Delete adAffectCurrent
rst.Update
rst.Close

我收到运行时错误3021,但查询不为空。

1 个答案:

答案 0 :(得分:6)

  

“我收到运行时错误3021,但查询不为空。”

仔细检查这一点。

Dim strSelect As String
strSelect = "SELECT * FROM Equipas " & _
    "WHERE ([ID - Funcionário] LIKE '" & _
    idfunc & "' AND [ID - Tarefa] LIKE ' " & idtask & "' );"
Debug.Print strSelect
rst.Open strSelect, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
If rs.BOF And rs.EOF Then
    MsgBox "recordset is empty"
Else
    rs.MoveLast
    MsgBox "recordset contains " & rs.RecordCount & " rows"
End If
'rst.Delete adAffectCurrent
'rst.Update
rst.Close

如果该版本的代码告诉您“记录集为空”,请转到立即窗口( Ctrl + g )进行检查代码构建的SELECT语句。您可以复制语句文本并将其粘贴到新的Access查询的SQL视图中以进行测试。

我最好的猜测是查询不返回任何行,因为它在idtask的值之前包含一个空格,并且没有[ID - Tarefa]值与空格加idtask匹配:

idfunc & "' AND [ID - Tarefa] LIKE ' " & idtask & "' );"
                                    ^ here