patriciaxxx
Programmer
I have the following code which writes the changes between to recordsets into a third table, and it works, except for 2 things which I can’t work out.
1 It doesn’t retrieve changes if there is a null or empty value.
2 It doesn’t retrieve Case Sensitive changes.
Any help modifying code to achieve this would be appreciated.
1 It doesn’t retrieve changes if there is a null or empty value.
2 It doesn’t retrieve Case Sensitive changes.
Any help modifying code to achieve this would be appreciated.
Code:
Option Compare Database
Option Explicit
'This requires a reference to the Microsoft DAO 3.6 Object Library.
Function CompareTables()
'Loop through each field in each record in the first table and compare it to the same field
'in the new table via the Primary Key.
Dim db As DAO.Database
Dim rstOLD As DAO.Recordset
Dim rstNEW As DAO.Recordset
Dim rstResults As DAO.Recordset
Dim fldName As String
Set db = CurrentDb
Set rstResults = db.OpenRecordset("tblResults") 'Results table.
Set rstOLD = db.OpenRecordset("tblBenchmarkSettings") 'Old table.
Do Until rstOLD.EOF
Set rstNEW = db.OpenRecordset("SELECT * " & _
"FROM tblBenchmarkSettings_2 " & _
"WHERE [NumberID]=" & rstOLD![NumberID]) '='" & rstOLD![pk string] & "';") 'New table.
If Not rstNEW.EOF And Not rstNEW.BOF Then
Dim i As Long
For i = 0 To rstOLD.Fields.Count - 1
fldName = rstOLD(i).Name
'Contents of field have changed.
If rstOLD(fldName) <> rstNEW(fldName) Then
'Write the differences to our table.
With rstResults
.AddNew
!PrimaryKey = rstOLD!NumberID
!fldName = fldName
!OldValue = rstOLD.Fields(fldName)
!NewValue = rstNEW.Fields(fldName)
!DateDetected = Now()
.Update
End With
End If
Next i
End If
rstOLD.MoveNext
Loop
'Clean up.
rstOLD.Close
rstNEW.Close
rstResults.Close
Set rstOLD = Nothing
Set rstNEW = Nothing
Set rstResults = Nothing
End Function