I have this code, that I am trying to filter and compare data from tables, the inner loop runs the correct number of times, but the outer only runs once and I cannot figure out why.
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
I tried to advnace the rstB in between the two ending loop statements like:
rstB.MoveNext
but the compiler said that the recordset was empty.
EDIT FINAL CODE:
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
rstFiltered=rstBand then loop through rstFiltered until EOF.rstBandrstFilteredboth point to the same recordset, so your outer loop only runs once... And where doesrstAcome from?rstBandrstFilteredare the same object.