thabounceisback
Registered User.
- Local time
- Today, 09:38
- Joined
- Sep 17, 2009
- Messages
- 31
Applying Audit trail Module to Multiple Controls in a Form
Greetings!
I have a module that is designed to create an audit trail for me. Here is the code below:
I am using the Forms Before Update event with the code:
to call the module into action.
However, I cannot figure out how to write the code so that it will audit several different controls on the form.
The controls I want it to audit are EmpCmb, Hours_Missed, DscCmb, and NtfCmb. Also, if possible, I would like it to audit my date picker control (MSComCtl2.DTPicker.2); although this is less important.
I have only been able to audit one control at a time. What am I doing wrong? What is my first step?
Greetings!
I have a module that is designed to create an audit trail for me. Here is the code below:
Code:
Option Compare Database
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, acComboBox
If .Value <> .OldValue Then
varBefore = .OldValue
varAfter = .Value
strControlName = .Name
'Build INSERT INTO statement.
strSQL = "INSERT INTO " _
& "Audit (EditDate, User, RecordID, SourceTable, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (Now()," _
& cDQ & Environ("username") & 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
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
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Call AuditTrail(Me, EmpCmb)
End Sub
However, I cannot figure out how to write the code so that it will audit several different controls on the form.
The controls I want it to audit are EmpCmb, Hours_Missed, DscCmb, and NtfCmb. Also, if possible, I would like it to audit my date picker control (MSComCtl2.DTPicker.2); although this is less important.
I have only been able to audit one control at a time. What am I doing wrong? What is my first step?
Last edited: