EOF / BOF错误 - Excel VBA

时间:2015-07-15 17:39:20

标签: excel-vba vba excel

当我尝试从excel文件中查询数据库时,我收到以下错误:

Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.

为什么弹出此错误?我已经在数据库中测试了我的SQL,它没有问题(我已经检查了4次,我没有打字)。

我计划将这些数据的特定行附加到列表框,并使用其余数据执行各种其他操作。为了测试我的SQL,我想我会迈出第一步。

我的代码:

Private Sub UserForm_Initialize()
    Check = True
    On Error GoTo UserForm_Initialize_Err
    Dim cnn As Object
    Dim rst As Object
    Dim StrDBPath As String
    Dim strSQL As String
    Set cnn = CreateObject("ADODB.Connection")
    Set rst = CreateObject("ADODB.Recordset")
    strSQL = "SELECT [Heads A].[Date Entered], [Heads A Issues].Department, [Heads A Issues].Equipment, [Heads A Issues].[Operation Issues], Sum([Heads A Issues].Downtime) AS SumOfDowntime1, IIf([Head A Crew]='3','C-Crew',IIf([Head A Crew]='2','B-Crew','A-Crew')) AS Crew " & _
            "FROM [Heads A] INNER JOIN [Heads A Issues] ON [Heads A].[HeadLineA ID] = [Heads A Issues].[HeadLineA ID]" & _
            " GROUP BY [Heads A].[Date Entered], [Heads A Issues].Department, [Heads A Issues].Equipment, [Heads A Issues].[Operation Issues], IIf([Head A Crew]='3','C-Crew',IIf([Head A Crew]='2','B-Crew','A-Crew'))" & _
            " HAVING ((([Heads A].[Date Entered])>=#" & Sheets("Choices").Cells(2, 1).Value & "# And ([Heads A].[Date Entered])<=#" & Sheets("Choices").Cells(2, 2).Value & "#) And (([Heads A Issues].Department)='" & Sheets("Choices").Cells(2, 3).Value & "') And ((IIf([Head A Crew]='3','C-Crew',IIf([Head A Crew]='2','B-Crew','A-Crew'))) Like IIf('" & Sheets("Choices").Cells(2, 4).Value & "'='all','*-Crew','" & _
            Sheets("Choices").Cells(2, 4).Value & "'))) ORDER BY [Heads A Issues].Department, [Heads A Issues].Equipment;"

    StrDBPath = Application.ActiveWorkbook.Path & "\Daily Closing Report V997.accdb"
     cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                             "Data Source=" & StrDBPath & ";" & _
                             "Jet OLEDB:Engine Type=5;" & _
                             "Persist Security Info=False;"
     rst.Open strSQL, _
              cnn, adOpenStatic
     rst.MoveFirst
     With Me.ListBox1
         .Clear
         Do
             .AddItem rst![Department]
             rst.MoveNext
         Loop Until rst.EOF
     End With
UserForm_Initialize_Exit:
     On Error Resume Next
     rst.Close
     cnn.Close
     Set rst = Nothing
     Set cnn = Nothing
     Exit Sub
UserForm_Initialize_Err:
     MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
     Resume UserForm_Initialize_Exit
End Sub

1 个答案:

答案 0 :(得分:1)

我不知道你在哪一行得到错误我认为它在本节

 rst.MoveFirst
 With Me.ListBox1
     .Clear
     Do
         .AddItem rst![Department]
         rst.MoveNext
     Loop Until rst.EOF
 End With

我会做出这些改变。

 If rst.recordcount > 0 then
     rst.MoveFirst
 End if
 With Me.ListBox1
     .Clear
     Do while rst.eof = false
         .AddItem rst![Department]
         rst.MoveNext
     Loop
 End With

我还建议您在记录集上使用锁。请查看adLockOptimistic和adlockpessimistic。

rst.Open strSQL, cnn, adOpenStatic, adLockOptimistic 

要回答你的上一条评论,我会尝试宣布不同的对象。并不是说你做的方式有任何问题,因为它没有产生任何错误。试试这个

Dim cnn As new adodb.connection
Dim rst As new adodb.recordset

这是早期绑定。所以你可以取出set cnn = CreateObject()并设置rst = CreateObject() 看看能不能在任何地方找到你。