I am using using an audit trail to track multiple fields that include textboxes and combo boxes. The audit trail is working perfectly in one database, but when I try to duplicate it in a different database it doesn't work so well.
In the one where it works I am using . . .the following code with the "bolded" portion being the part used to track changes in all fields
'Get changed values.
For Each ctl In MyForm.Controls
With ctl
Select Case ctl.ControlTypeCase acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox'If .OldValue = "" Or IsNull(.OldValue) Then
'varBefore = "Empty"
'MsgBox ctl.Name
'End If
If (.Value <> .OldValue) Or ((Not IsNull(.OldValue) And IsNull(.Value))) Or ((IsNull(.OldValue) And Not IsNull(.Value))) Then
varBefore = .OldValue
varAfter = .Value
strControlName = .Name
'Build INSERT INTO statement.
strSQL = "INSERT INTO " _
& "AuditTrail (EditDate, User, RecordID, SourceTable, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (Now()," _
& cDQ & Environ("username") & cDQ & ", " _
& cDQ & recordid.Value & cDQ & ", " _
& cDQ & MyForm.RecordSource & cDQ & ", " _
& cDQ & .Name & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
'View evaluated statement in Immediate window.
Debug.Print strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End If
End Select
End With
Next
Set ctl = Nothing
Exit Sub
ErrHandler:
If Err.Number <> 3251 Then MsgBox Err.Description & vbNewLine & Err.Number, vbOKOnly, "Error Here"
Exit Sub
End Sub
When I use this same code in a different database it doesn't work . . .I get the error "You entered an expression that has no value 2427". Yet if i change the code to track textbx only it works perfectly. The problem is, the form has fields with textbx and
combobx so I need it to track both.
Also, when I change a value in the combo box I get the code "Type mismatch 13" although it still records the change in the audit trail table.
Thanks in advance for any suggestions.