D
DawnTreader
Hello All
I have an app that i was thinking about auditing, and i looked at the code
Allen Browne has, and then this code a former collegue was using:
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
MsgBox "Auditing"
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, acOptionGroup, acComboBox,
acOptionButton, acToggleButton:
MsgBox "value: " & .Value
If .Value <> .OldValue Then
varBefore = .OldValue
varAfter = .Value
strControlName = .Name
'Build INSERT INTO statement.
strSQL = "INSERT INTO " _
& "tblAudit (DateEdited, UserChangedById, RecordID,
SourceTable, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (#" & Now() & "#," _
& cDQ & Forms!zzMAINFORM!cboEmployee & cDQ & ", " _
& cDQ & recordid.Value & cDQ & ", " _
& cDQ & frm.RecordSource & cDQ & ", " _
& cDQ & .Name & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
'View evaluated statement in Immediate window.
'Debug.Print strSQL
MsgBox strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End If
End Select
End With
Next
Set ctl = Nothing
Exit Sub
ErrHandler:
MsgBox Err.DESCRIPTION & vbNewLine & Err.Number, vbOKOnly, "Error"
End Sub
The code above is a little less complicated, and seems to work. is there
anything that would be a problem? i have already noticed that it doesnt work
on subforms, so for every form the user can see at the time it would be
necessary to put the code on the form.
another problem i noticed is that when i have filter fields that i use to
allow users to put in criteria for a search in a list box or datasheet
subform, it seems to think those are valid changes and wants to record them
but doesnt. i assume that it doesnt because they are unbound, but it is
something that causes a little more processing time. i was thinking of
finding a way to "tag" those fields and tell the code to ignore them. any
ideas on that?
as always, any and all help appreciated.
I have an app that i was thinking about auditing, and i looked at the code
Allen Browne has, and then this code a former collegue was using:
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
MsgBox "Auditing"
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, acOptionGroup, acComboBox,
acOptionButton, acToggleButton:
MsgBox "value: " & .Value
If .Value <> .OldValue Then
varBefore = .OldValue
varAfter = .Value
strControlName = .Name
'Build INSERT INTO statement.
strSQL = "INSERT INTO " _
& "tblAudit (DateEdited, UserChangedById, RecordID,
SourceTable, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (#" & Now() & "#," _
& cDQ & Forms!zzMAINFORM!cboEmployee & cDQ & ", " _
& cDQ & recordid.Value & cDQ & ", " _
& cDQ & frm.RecordSource & cDQ & ", " _
& cDQ & .Name & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
'View evaluated statement in Immediate window.
'Debug.Print strSQL
MsgBox strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End If
End Select
End With
Next
Set ctl = Nothing
Exit Sub
ErrHandler:
MsgBox Err.DESCRIPTION & vbNewLine & Err.Number, vbOKOnly, "Error"
End Sub
The code above is a little less complicated, and seems to work. is there
anything that would be a problem? i have already noticed that it doesnt work
on subforms, so for every form the user can see at the time it would be
necessary to put the code on the form.
another problem i noticed is that when i have filter fields that i use to
allow users to put in criteria for a search in a list box or datasheet
subform, it seems to think those are valid changes and wants to record them
but doesnt. i assume that it doesnt because they are unbound, but it is
something that causes a little more processing time. i was thinking of
finding a way to "tag" those fields and tell the code to ignore them. any
ideas on that?
as always, any and all help appreciated.