Hi Guys,
I found this code to compare tables in my database and it works great untill it hits this records when I get an error message:
"syntax error (missing operator) in query expression ''O'Neill ');'."
Can anyone see where the problem is?
thanks in advance.
Sub CompareTables2(BaseTable As String, PrimaryKeyField As String, _
BaseTableQuery As String, VaryingTableQuery As String)
'parameters
' BaseTable: the table that is considered the 'base', that is, the one considered accurate
' PrimaryKeyField: the primary key for both tables
' qryBase: an ordered query based on the 'base' table
' qryVarying: an ordered query based on the table being compared to the 'base'
On Error GoTo Err_CompareTables
Dim db As dao.Database
Dim rstBase As dao.Recordset
Dim rstVarying As dao.Recordset
Dim tdf As dao.TableDef
Dim fld As dao.Field
Dim FieldChanged As Boolean
Dim ErrorMessage As String
Set db = CurrentDb
Set rstBase = db.OpenRecordset(BaseTableQuery)
Set rstVarying = db.OpenRecordset(VaryingTableQuery)
Set tdf = db.TableDefs(BaseTable)
db.TableDefs.Delete "TableDiscrepancies2"
db.Execute ("CREATE TABLE TableDiscrepancies2 (RecordNumber TEXT(255), FieldName TEXT(255), OldText TEXT(255), NewText TEXT(255));")
rstBase.MoveFirst
rstVarying.MoveFirst
Do Until rstBase.EOF
If rstVarying.EOF = True Then
ErrorMessage = "**** record " & rstBase(PrimaryKeyField) & _
" deleted ****"
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber) " _
& "VALUES) ( '" & ErrorMessage & "');")
For Each fld In tdf.Fields
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber, FieldName, OldText)" _
& " VALUES ( '" & rstBase(PrimaryKeyField) & "','" & fld.Name & "','" & rstBase(fld.Name) & "');")
FieldChanged = True
Next fld
rstBase.MoveNext
ElseIf rstBase(PrimaryKeyField) > rstVarying(PrimaryKeyField) Then
ErrorMessage = "**** record " & rstVarying(PrimaryKeyField) & _
" added ****"
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber) " _
& "VALUES ( '" & ErrorMessage & "');")
For Each fld In tdf.Fields
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber, FieldName, NewText)" _
& " VALUES ( '" & rstVarying(PrimaryKeyField) & "','" & fld.Name & "','" & rstVarying(fld.Name) & "');")
FieldChanged = True
Next fld
rstVarying.MoveNext
ElseIf rstBase(PrimaryKeyField) < rstVarying(PrimaryKeyField) Then
ErrorMessage = "**** record " & rstBase(PrimaryKeyField) & _
" deleted ****"
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber) " _
& "VALUES ( '" & ErrorMessage & "');")
For Each fld In tdf.Fields
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber, FieldName, OldText)" _
& " VALUES ( '" & rstBase(PrimaryKeyField) & "','" & fld.Name & "','" & rstBase(fld.Name) & "');")
FieldChanged = True
Next fld
rstBase.MoveNext
Else
FieldChanged = False
For Each fld In tdf.Fields
If Nz(rstBase(fld.Name)) <> Nz(rstVarying(fld.Name)) Then
If Not FieldChanged Then
ErrorMessage = "**** record " & rstBase(PrimaryKeyField) & _
" Modified ****"
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber) " _
& "VALUES ( '" & ErrorMessage & "');")
End If
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber, FieldName, OldText, NewText)" _
& " VALUES ( '" & rstBase(PrimaryKeyField) & "','" & fld.Name & "','" & Nz(rstBase(fld.Name), "<Null>") & "','" & Nz(rstVarying(fld.Name), "<Null>") & "');")
FieldChanged = True
End If
Next fld
' If Not FieldChanged Then
' ErrorMessage = "Record " & rstBase(PrimaryKeyField) & _
' " identical"
' End If
rstBase.MoveNext
rstVarying.MoveNext
FieldChanged = False
End If
Loop
Do Until rstVarying.EOF
ErrorMessage = "**** record " & rstVarying(PrimaryKeyField) & _
" added to ****"
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber) " _
& "VALUES ( '" & ErrorMessage & "');")
For Each fld In tdf.Fields
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber, FieldName, NewText)" _
& " VALUES ( '" & rstVarying(PrimaryKeyField) & "','" & fld.Name & "','" & rstVarying(fld.Name) & "');")
FieldChanged = True
Next fld
rstVarying.MoveNext
Loop
Exit_CompareTables:
Set rstBase = Nothing
Set rstVarying = Nothing
Debug.Print "Done."
db.Close
Exit Sub
Err_CompareTables:
If Err.Number = 3010 Then '*** if the error is the table is missing
Resume Next '*** then skip the delete line and resume on the next line
ElseIf Err.Number = 3265 Then
Resume Next
Else
MsgBox Err.Description '*** write out the error and exit the sub
Resume Exit_CompareTables
End If
Debug.Print "Done"
End Sub
I found this code to compare tables in my database and it works great untill it hits this records when I get an error message:
"syntax error (missing operator) in query expression ''O'Neill ');'."
Can anyone see where the problem is?
thanks in advance.
Sub CompareTables2(BaseTable As String, PrimaryKeyField As String, _
BaseTableQuery As String, VaryingTableQuery As String)
'parameters
' BaseTable: the table that is considered the 'base', that is, the one considered accurate
' PrimaryKeyField: the primary key for both tables
' qryBase: an ordered query based on the 'base' table
' qryVarying: an ordered query based on the table being compared to the 'base'
On Error GoTo Err_CompareTables
Dim db As dao.Database
Dim rstBase As dao.Recordset
Dim rstVarying As dao.Recordset
Dim tdf As dao.TableDef
Dim fld As dao.Field
Dim FieldChanged As Boolean
Dim ErrorMessage As String
Set db = CurrentDb
Set rstBase = db.OpenRecordset(BaseTableQuery)
Set rstVarying = db.OpenRecordset(VaryingTableQuery)
Set tdf = db.TableDefs(BaseTable)
db.TableDefs.Delete "TableDiscrepancies2"
db.Execute ("CREATE TABLE TableDiscrepancies2 (RecordNumber TEXT(255), FieldName TEXT(255), OldText TEXT(255), NewText TEXT(255));")
rstBase.MoveFirst
rstVarying.MoveFirst
Do Until rstBase.EOF
If rstVarying.EOF = True Then
ErrorMessage = "**** record " & rstBase(PrimaryKeyField) & _
" deleted ****"
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber) " _
& "VALUES) ( '" & ErrorMessage & "');")
For Each fld In tdf.Fields
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber, FieldName, OldText)" _
& " VALUES ( '" & rstBase(PrimaryKeyField) & "','" & fld.Name & "','" & rstBase(fld.Name) & "');")
FieldChanged = True
Next fld
rstBase.MoveNext
ElseIf rstBase(PrimaryKeyField) > rstVarying(PrimaryKeyField) Then
ErrorMessage = "**** record " & rstVarying(PrimaryKeyField) & _
" added ****"
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber) " _
& "VALUES ( '" & ErrorMessage & "');")
For Each fld In tdf.Fields
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber, FieldName, NewText)" _
& " VALUES ( '" & rstVarying(PrimaryKeyField) & "','" & fld.Name & "','" & rstVarying(fld.Name) & "');")
FieldChanged = True
Next fld
rstVarying.MoveNext
ElseIf rstBase(PrimaryKeyField) < rstVarying(PrimaryKeyField) Then
ErrorMessage = "**** record " & rstBase(PrimaryKeyField) & _
" deleted ****"
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber) " _
& "VALUES ( '" & ErrorMessage & "');")
For Each fld In tdf.Fields
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber, FieldName, OldText)" _
& " VALUES ( '" & rstBase(PrimaryKeyField) & "','" & fld.Name & "','" & rstBase(fld.Name) & "');")
FieldChanged = True
Next fld
rstBase.MoveNext
Else
FieldChanged = False
For Each fld In tdf.Fields
If Nz(rstBase(fld.Name)) <> Nz(rstVarying(fld.Name)) Then
If Not FieldChanged Then
ErrorMessage = "**** record " & rstBase(PrimaryKeyField) & _
" Modified ****"
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber) " _
& "VALUES ( '" & ErrorMessage & "');")
End If
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber, FieldName, OldText, NewText)" _
& " VALUES ( '" & rstBase(PrimaryKeyField) & "','" & fld.Name & "','" & Nz(rstBase(fld.Name), "<Null>") & "','" & Nz(rstVarying(fld.Name), "<Null>") & "');")
FieldChanged = True
End If
Next fld
' If Not FieldChanged Then
' ErrorMessage = "Record " & rstBase(PrimaryKeyField) & _
' " identical"
' End If
rstBase.MoveNext
rstVarying.MoveNext
FieldChanged = False
End If
Loop
Do Until rstVarying.EOF
ErrorMessage = "**** record " & rstVarying(PrimaryKeyField) & _
" added to ****"
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber) " _
& "VALUES ( '" & ErrorMessage & "');")
For Each fld In tdf.Fields
db.Execute ("INSERT INTO TableDiscrepancies2 (RecordNumber, FieldName, NewText)" _
& " VALUES ( '" & rstVarying(PrimaryKeyField) & "','" & fld.Name & "','" & rstVarying(fld.Name) & "');")
FieldChanged = True
Next fld
rstVarying.MoveNext
Loop
Exit_CompareTables:
Set rstBase = Nothing
Set rstVarying = Nothing
Debug.Print "Done."
db.Close
Exit Sub
Err_CompareTables:
If Err.Number = 3010 Then '*** if the error is the table is missing
Resume Next '*** then skip the delete line and resume on the next line
ElseIf Err.Number = 3265 Then
Resume Next
Else
MsgBox Err.Description '*** write out the error and exit the sub
Resume Exit_CompareTables
End If
Debug.Print "Done"
End Sub