Here is a procedure that will compare 2 tables. It will compare table1 field1 with table2 field1...
Output shows the record number, the keyfield, field name and value of tableA and tableB in immediate window
Code:
'---------------------------------------------------------------------------------------
' Procedure : Compare2TableFieldByField
' Author : Jack
' Date : 17/03/2014
' Purpose : Compare 2 tables field by field. Display non matches in the immediate window.
'
' Output shows the record number, the keyfield, field name and value of tableA and tableB
'---------------------------------------------------------------------------------------
'
Sub Compare2TableFieldByField(tblAName As String, tblBName As String, iPosKeyFld As Integer)
Dim db As dao.Database
Dim i As Integer
Dim J As Integer
Dim K As Integer
Dim RecNo As Integer
Dim rsA As dao.Recordset
Dim rsB As dao.Recordset
Dim strA As String
Dim strB As String
10 On Error GoTo Compare2TableFieldByField_Error
20 Set db = CurrentDb
30 Set rsA = db.OpenRecordset(tblAName, dbOpenSnapshot)
40 Set rsB = db.OpenRecordset(tblBName, dbOpenSnapshot)
50 Debug.Print "RecNo " & " keyValue" & Space(7) & "fieldA" & Space(15) & " ValueA" & Space(25) & "ValueB"
60 Do While Not rsA.EOF
'check each field in RSA
70 RecNo = RecNo + 1 'count each record in customerA
80 For i = 0 To rsA.Fields.Count - 1
'some values are Null
90 strA = IIf(Trim(rsA.Fields(i).Value) = "", rsA.Fields(i).Value, "-Spaces-")
100 strA = IIf(IsNull(rsA.Fields(i).Value), "??? NULL ???", rsA.Fields(i).Value)
110 strB = IIf(IsNull(rsB.Fields(i).Value), "??? NULL ???", rsB.Fields(i).Value)
120 strA = Replace(strA, vbCrLf, " ")
130 strB = Replace(strB, vbCrLf, " ")
'report differences
140 If strA <> strB Then
150 Debug.Print RecNo & Space(6) & rsA.Fields(iPosKeyFld).Value & Space(11) & rsA.Fields(i).name & Space(20 - Len(rsA.Fields(i).name)) & strA & Space(30 - Len(strA)) & strB
160 End If
170 Next i
180 rsA.MoveNext
190 rsB.MoveNext
200 Loop
210 On Error GoTo 0
220 Exit Sub
Compare2TableFieldByField_Error:
230 MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure Compare2TableFieldByField of Module AWF_Related"
End Sub
Here is a test routine showing how to call the procedure
Code:
Sub testcompare()
Dim a As String
Dim b As String
Dim i As Integer
a = "CustomerA"
b = "CustomerB"
i = 0
Call Compare2TableFieldByField(a, b, i)
End Sub
Here is the output from my test tables
Code:
RecNo keyValue fieldA ValueA ValueB
3 ANTON ContactTitle Owner Partner
6 BLAUS ContactName Hanna Moos Hanna Moose
16 CONSH ContactName Elizabeth Brown Beth Brown
16 CONSH Address Berkeley Gardens 12 Brewery Cornington Square 12 Brewery
16 CONSH Phone (171) 555-2282 (171) 456-2282
16 CONSH Fax (171) 555-9199 (171) 545-9199
22 FISSA ContactName Diego Roel Rosa Martinez
24 FOLKO City Bräcke Stockholm
25 FRANK ContactName Peter Franken Pete Franken
39 KOENE ContactName Philip Cramer Phillip Cramer
42 LAUGB Address 1900 Oak St. 1900 Oak Street
48 LONEP Region OR Oregon
51 MEREP ContactName Jean Fresnière J. Fresnière
If you copy the test routine and modify it as below and set i = the position of your keyfield (using 0,1,2,3...) 0 based
Code:
Sub testcompare()
Dim a As String
Dim b As String
Dim i As Integer
a = "TableA"
b = "TableB"
i = 0
Call Compare2TableFieldByField(a, b, i)
End Sub