I'm implementing someone else's code for creating an audit trail for data edits. I REALLY need this or something similar to track changes made to my the data in my forms.
The first time I put it in, it worked great... I tried in another database and started getting ByRef argument type mismatch errors... I had to go back and modify the original database and now I'm getting "Compile Error: type mismatch" errors. I'm going insane with this!
I'm a very novice programmer...
Thanks in advance for any insight!
Here's the code I'm using:
In the form:
[CODE=vb]Private Sub Form_BeforeUpda te(Cancel As Integer)
Call AuditTrail(Me, RecordNo) 'RecordNo is the primary key for each table
End Sub[/CODE]
In basAuditTrail:
[CODE=vb]Const cDQ As String = """"
Sub AuditTrail(frm As Form, recordid As Control)
'Track changes to data.
'recordid identifies the pk field's corresponding
'control in frm, in order to id record.
Dim ctl As Control
Dim varBefore As Variant
Dim varAfter As Variant
Dim strControlName As String
Dim strSQL As String
On Error GoTo ErrHandler
'Get changed values.
For Each ctl In frm.Controls
With ctl
'Avoid labels and other controls with Value property.
Select Case ctl.ControlType
Case acTextBox, acCheckBox, acComboBox
If .Value <> .OldValue Then
varBefore = .OldValue
varAfter = .Value
strControlName = .Name
'Build INSERT INTO statement.
strSQL = "INSERT INTO " _
& "tblAudit (EditDate, User, RecordID, SourceTable, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (Now()," _
& cDQ & Environ("userna me") & cDQ & ", " _
& cDQ & recordid.Value & cDQ & ", " _
& cDQ & frm.RecordSourc e & cDQ & ", " _
& cDQ & .Name & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
'View evaluated statement in Immediate window.
Debug.Print strSQL
DoCmd.SetWarnin gs False
DoCmd.RunSQL strSQL
DoCmd.SetWarnin gs True
End If
End Select[/CODE]
The first time I put it in, it worked great... I tried in another database and started getting ByRef argument type mismatch errors... I had to go back and modify the original database and now I'm getting "Compile Error: type mismatch" errors. I'm going insane with this!
I'm a very novice programmer...
Thanks in advance for any insight!
Here's the code I'm using:
In the form:
[CODE=vb]Private Sub Form_BeforeUpda te(Cancel As Integer)
Call AuditTrail(Me, RecordNo) 'RecordNo is the primary key for each table
End Sub[/CODE]
In basAuditTrail:
[CODE=vb]Const cDQ As String = """"
Sub AuditTrail(frm As Form, recordid As Control)
'Track changes to data.
'recordid identifies the pk field's corresponding
'control in frm, in order to id record.
Dim ctl As Control
Dim varBefore As Variant
Dim varAfter As Variant
Dim strControlName As String
Dim strSQL As String
On Error GoTo ErrHandler
'Get changed values.
For Each ctl In frm.Controls
With ctl
'Avoid labels and other controls with Value property.
Select Case ctl.ControlType
Case acTextBox, acCheckBox, acComboBox
If .Value <> .OldValue Then
varBefore = .OldValue
varAfter = .Value
strControlName = .Name
'Build INSERT INTO statement.
strSQL = "INSERT INTO " _
& "tblAudit (EditDate, User, RecordID, SourceTable, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (Now()," _
& cDQ & Environ("userna me") & cDQ & ", " _
& cDQ & recordid.Value & cDQ & ", " _
& cDQ & frm.RecordSourc e & cDQ & ", " _
& cDQ & .Name & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
'View evaluated statement in Immediate window.
Debug.Print strSQL
DoCmd.SetWarnin gs False
DoCmd.RunSQL strSQL
DoCmd.SetWarnin gs True
End If
End Select[/CODE]
Comment