外环运行1次VBA

时间:2013-07-22 15:47:51

标签: vba access-vba

我有这个代码,我试图过滤和比较表中的数据,内循环运行正确的次数,但外部只运行一次,我无法弄清楚原因。

Do While Not rstA.EOF

' Gets the first serial for filtering
rstA.MoveFirst
serialNumber = rstB!serial
rstB.Filter = "serial = '" & serialNumber & "'"
' A nested loop for the filtered rst and goes til the end of that
Do While Not rstB.EOF

    If rstA.Fields("serial") = rstB.Fields("serial") Then

        If rstA.Fields("accountnumber") <> rstB.Fields("accountnumber") Then

            'Print Message

        ElseIf rstA.Fields("model_number") <> rstB.Fields("model_number") Then

            'Print Message

        End If

    Else
    ' This will always be the default until there are two matching serial numbers
    ' This will advnace rstA +1 and make rstFiltered stay put
    ' I had to work around the advancements on the outside of this statement
    ' Thats why I move rstA (+1 = 1) and rstFiltered (-1 +1 = 0)
        'Print message
        rstB.MovePrevious
    End If
    rstB.MoveNext
    rstA.MoveNext
Loop
Loop

我试图在两个结束循环语句之间建立rstB,如:

rstB.MoveNext

但是编译器说记录集是空的。

编辑最终代码:

Do Until rstB.EOF

Do Until rstA.EOF
    If rstA.Fields("serial") = rstB.Fields("serial") Then
        If rstA.Fields("accountnumber") <> rstB.Fields("accountnumber") Then
            accountMessage = "Account number A, " & rstA.Fields("accountnumber") & ", and Account " _
            & "number B, " & rstB.Fields("accountnumber") & ", for serial number ," & rstB.Fields("serial") & ", do not match."
            Debug.Print accountMessage
        ElseIf rstA.Fields("model_number") <> rstB.Fields("model_number") Then
            accountMessage = "Model number A, " & rstA.Fields("model_number") & ", and Model " _
            & "number B, " & rstB.Fields("model_number") & ", for serial number ," & rstB.Fields("serial") & ", do not match."
            Debug.Print accountMessage
        End If
    Else
    ' This will always be the default until there are two matching serial numbers
    ' This will advnace rstA +1 and make rstFiltered stay put
    ' I had to work around the advancements on the outside of this statement
    ' Thats why I move rstA (+1 = 1) and rstFiltered (-1 +1 = 0)
        rstB.MoveNext
        rstA.MovePrevious
    End If
    rstA.MoveNext
Loop
rstB.MoveNext
Loop

2 个答案:

答案 0 :(得分:1)

你的第一个内圈rstA.MoveNext中的最后一行是让你的第一个外环达到EOF并且只运行一次。

根据你对Dick的问题的评论,这是对你想要完成的事情的公平假设吗?

看起来你要做的就是通过表B,获取序列号,检查表A中的记录。

假设每个序列号在B中只有一条记录,因为如果A不匹配则将B保持在相同位置,以便循环继续。

Do Until rstB.EOF

    Do Until rstAB.EOF
        If rstA.Fields("serial") = rstB.Fields("serial") Then
            If rstA.Fields("accountnumber") <> rstB.Fields("accountnumber") Then
                'Print Message
            ElseIf rstA.Fields("model_number") <> rstB.Fields("model_number") Then
                'Print Message
            End If
        End If
        rstA.MoveNext
    Loop

    rstB.MoveNext
Loop

答案 1 :(得分:0)

当你将rstB过滤到(大概)一条记录时,它会在第一遍后达到EOF。您必须克隆rstB然后过滤克隆。但似乎您可以使用查询来获取所需的记录。

Sub Comparerss()

    Dim rstFiltered As ADODB.Recordset
    Dim aSql(1 To 4) As String

    Set rstFiltered = New ADODB.Recordset

    'outer join were tblB stuff is Null means they don't match tblA
    aSql(1) = "SELECT tblA.Serial"
    aSql(2) = "FROM tblA LEFT JOIN tblB ON (tblA.ModelNumber = tblB.ModelNumber)"
    aSql(3) = "AND (tblA.AccountNumber = tblB.AccountNumber) AND (tblA.Serial = tblB.Serial)"
    aSql(4) = "WHERE (((tblB.AccountNumber) Is Null) AND ((tblB.ModelNumber) Is Null));"

    rstFiltered.Open Join(aSql, Space(1)), CurrentProject.Connection

    Do While Not rstFiltered.EOF
        Debug.Print rstFiltered.Fields("Serial").Value
        rstFiltered.MoveNext
    Loop

End Sub