简体   繁体   English

BOF或EOF为True。 在完成之前包括EOF

[英]Either BOF or EOF is True. Include EOF in Do Until

I'm getting an error at rs.Movenext in my For Each. 我在For Each中的rs.Movenext出现错误。 I've tried using a Do Until but not sure where to place it. 我试过使用直到直到但不确定将其放置在何处。 Can someone advise where to place it? 有人可以建议将其放置在哪里吗? Not sure if using EOF in my Do Until will overcome the error though. 不知道在我的“直到”中使用EOF是否可以克服该错误。

Thanks 谢谢

    Sub FindCardOrdersv2()

    ' Initialize variables.
    Dim cn As ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim provStr As String
    Dim intMaxCol As Integer
    Dim intMaxRow As Integer
    Dim rsFilter As Range
    Dim i As Integer
    Dim rng As Variant
    Dim payid(1 To 10) As String
    Dim tw As ThisWorkbook
    Dim errmsg As String

    Workbooks("cleanse.xlsm").Activate
    Worksheets("Sheet1").Activate

    ' Create new instances
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset


    ' sql query
    sql = "SELECT TOP 100 t.tri_transactionidcode," _
          & "SUBSTRING(t.tri_reference, 1, 9) AS merchantref," _
          & "t.tri_additionalreferencenumber, t.CreatedOn, t.tri_amount, ISNULL(t.tri_paymenttransactiontypeidName, 'Online')" _
          & " FROM dbo.tri_onlinepayment t INNER JOIN dbo.tri_transaction tr ON tr.tri_onlinepaymentid = t.tri_onlinepaymentId" _
          & " WHERE t.tri_transactionresult = 9 AND t.tri_transactionidcode IN (1013302661,1013327345, 1013172653)"



    ' Specify the OLE DB provider.
    cn.Provider = "sqloledb"

    ' Specify connection string on Open method.
    cn.Open "Data Source=IFL-SQL11;Database=IFL_MSCRM;Trusted_Connection=yes;Integrated Security=SSPI"



    ' Assign active connection to recordset
    Set rs.ActiveConnection = cn
    'intMaxCol = rs.Fields.Count

    ' Define cursors and open sql
    With rs
        .CursorLocation = adUseServer
        .CursorType = adOpenStatic
        .LockType = adLockReadOnly
        .Open sql
    End With


    For i = 1 To rs.RecordCount
       If Not (rs.BOF And rs.EOF) Then
            payid(i) = rs.Fields.Item(0)
            Debug.Print rs(0)
            Debug.Print rs(1)
            Debug.Print rs(3)
        End If
        rs.MoveNext
    Next i

    errmsg = "No matches found"

    For Each rsFilter In Range("A1:A10").Cells
        For i = 1 To rsFilter.Cells.Count
          'Do Until rs.EOF
            If rsFilter.Value = payid(i) Then
                   Debug.Print rsFilter.Value
                   Debug.Print rsFilter.Offset(0, 1).Value
            Else: Debug.Print errmsg & " " & rsFilter.Value

            End If
              'Loop
                    rs.MoveNext
            'Loop
        Next i
    Exit For

Update 更新

Following your suggestion Fil, I'm getting an error at bold. 按照您的建议Fil,我在粗体显示时出现了错误。 Do I not need to use i as a counter? 我不需要使用i作为计数器吗? Sorry I'm a little confused because its the second For Each that I can't get to work properly. 抱歉,我有点困惑,因为它是我无法正常工作的第二个For Each。 The first For works fine as far as I know. 据我所知,第一个For可以正常工作。 Thanks 谢谢

'For i = 1 To rs.RecordCount
    'If Not (rs.BOF And rs.EOF) Then
    While Not rs.EOF
         **payid(i) = rs.Fields.Item(0)**
         Debug.Print rs(0)
         Debug.Print rs(1)
         Debug.Print rs(3)
     End If
     rs.MoveNext
   End While
'Next i


    **For Each rsFilter In Range("A1:A10").Cells
        For i = 1 To rsFilter.Cells.Count
          'Do Until rs.EOF
            If rsFilter.Value = payid(i) Then
                   Debug.Print rsFilter.Value
                   Debug.Print rsFilter.Offset(0, 1).Value
            Else: Debug.Print errmsg & " " & rsFilter.Value

            End If
              'Loop
                    rs.MoveNext
            'Loop
        Next i
    Exit For
    Next**

Update 2 更新2

I've fixed the BOF and EOF error I had at first by doing aa while not rs.eof before rs.movenext. 我已经修复了最初出现的BOF和EOF错误,方法是在rs.movenext之前先执行aa而不是rs.eof。 But when my If is true (barclays.value = payid(i) I get another BOF/EOF is true error when I try to enter the rs.fields.item(0) back to my range. Any suggestions? Thanks 但是,如果我的If为true(barclays.value = payid(i),当我尝试将rs.fields.item(0)返回到我的范围时,我得到另一个BOF / EOF为true的错误。

For Each barclays In Range("A1", Range("A1").End(xlDown)).Cells 
    For i = 1 To rs.RecordCount 
        If barclays.Value = payid(i) Then 
           barclays.Offset(0, 1) = rs.Fields.Item(0)
            Debug.Print barclays.Value 
            Debug.Print barclays.Offset(0, 1).Value 
        Else: 
            barclays.Offset(0, 1) = "No payment found for " & barclays.Value 
            Debug.Print "No payment found for " & barclays.Value & " for " & payid(i) 
        End If 
        While Not rs.EOF 
            rs.MoveNext 
        Wend 
    Next i 
Next 

I would change: 我会改变:

    'For i = 1 To rs.RecordCount
       'If Not (rs.BOF And rs.EOF) Then
       While Not rs.Eof
            payid(i) = rs.Fields.Item(0)
            Debug.Print rs(0)
            Debug.Print rs(1)
            Debug.Print rs(3)
        End If
        rs.MoveNext
      End While
    'Next i

The second for doesn't nothing on the recordset so you have to remove "rs.Movenext" instruction. 第二个for记录集上没有内容,因此您必须删除“ rs.Movenext”指令。 The "Exit For" outside a For is meaningless, so you have to remove that instruction too. For之外的“ Exit For”是没有意义的,因此您也必须删除该指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM