我希望得到您的帮助,为两个表之间的匹配提供解决方案,并返回不匹配的记录,并指定哪个字段不匹配。
记下每张表包含30多个字段。
答案 0 :(得分:0)
您可以使用记录集,但如果您的表很长,则可能需要一段时间。这绝对没有优化,但考虑到你提供的信息很少,我不想投入大量时间。
我假设你的表在结构上是相同的,排序相同,并且具有相同数量的记录。如果没有,请随意调整,但是你应该能够了解我正在做的事情。
当发现不匹配时,它将在即时窗口中输出Table1
的字段和行号。如果要恢复所有字段值,也可以将其插入临时表中,但同样,我不想走那么远。所以这有局限性:
Public Function FindMisMatches(Table1 As String, Table2 As String)
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim i As Integer
Dim Row As Integer
On Error GoTo PROC_ERR
Set db = CurrentDb
Set rs1 = db.OpenRecordset(Table1, dbOpenSnapshot, dbReadOnly)
Set rs2 = db.OpenRecordset(Table2, dbOpenSnapshot, dbReadOnly)
rs1.MoveFirst
rs2.MoveFirst
Row = 1
Do Until rs1.EOF Or rs2.EOF
'Assuming both tables have identical structure
For i = 1 To rs1.Fields.Count - 1
If rs1.Fields(i).Value <> rs2.Fields(i).Value Then
Debug.Print "Mismatch in field " & rs1.Fields(i).Name & " on row " & Row
End If
Next i
rs1.MoveNext
rs2.MoveNext
Row = Row + 1
Loop
Debug.Print "End of recordset"
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & "; " & Err.Description
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
End Function