我试图通过使用序列号作为主键来比较两个表。由于它们的大小可能不同,因此序列号可能在 SerialAccount_b 中,而不是在 SerialAccount_a 中。发生这种情况时,我会到达文件末尾,因为一个记录集会前进,而另一个记录集则保持原样以尝试查找序列。然后它返回错误 3021。我有错误处理,但我希望代码在发生这种情况后继续比较其余数据,但我在这样做时遇到了麻烦。如果你能帮助我,那就太好了。如果您对如何改进这个子程序有任何其他建议,那也很好。
Public Sub compareSerialAccount()
Dim dbs As DAO.Database
Dim rstA As DAO.Recordset
Dim rstB As DAO.Recordset
Dim rstFiltered As DAO.Recordset
Dim serialNumber As String
On Error GoTo HandleErrors
Set dbs = CurrentDb
counter1 = 0
' Opens the recordsets of SerialAccount_a and _b in
' preperation for comparison of data
Set rstA = dbs.OpenRecordset("SerialAccount_a", dbOpenDynaset)
Set rstB = dbs.OpenRecordset("SerialAccount_b", dbOpenDynaset)
Do Until rstB.EOF
Do Until rstA.EOF
If rstA.Fields("serial") = rstB.Fields("serial") Then
If rstA.Fields("accountnumber") <> rstB.Fields("accountnumber") Then
' Print message
counterForMessage = counterForMessage + 1
End If
If rstA.Fields("model_number") <> rstB.Fields("model_number") Then
' Print message
counterForMessage = counterForMessage + 1
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
If counterForMessage = 0 Then
Debug.Print "| There are no discrepancies between SerialAccount_a and SerialAccount_b! |"
End If
HandleErrors:
Select Case Err.Number
Case 3021
serialNumberForError = rstA.Fields("serial")
Debug.Print "| Serial number " & serialNumberForError & " was either added or deleted."
Resume Next
End Select
End Sub