Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Form_BeforeUpdate_Error
gApNo = Me.ApNo
gWSNO = Me.WS_No
Call AuditTrail(Me)
On Error GoTo 0
Exit Sub
Form_BeforeUpdate_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_BeforeUpdate of VBA Document Form_FE_WS_Main"
End Sub
Option Compare Database
Option Explicit
Const cDQ As String = """"
Sub AuditTrail(frm As Form)
'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
For Each ctl In frm.Controls
If ctl.Tag = "Audit" Then
varBefore = Nz(ctl.oldvalue)
Debug.Print varBefore
varAfter = Nz(ctl.Value)
Debug.Print varAfter
If varAfter <> varBefore Then
strControlName = Nz(ctl.Name)
Debug.Print ctl.Name
'Build INSERT INTO statement.
strSQL = "INSERT INTO " _
& "Audit (EditDate, User, ApNo, WSNO, MeasNO, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (Now()," _
& cDQ & Environ("username") & cDQ & ", " _
& cDQ & GetgApNo() & cDQ & ", " _
& cDQ & GetgWSNO() & cDQ & ", " _
& cDQ & GetgMeasNo() & cDQ & ", " _
& cDQ & strControlName & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
DoCmd.RunSQL strSQL
Debug.Print strSQL
End If
End If
Next ctl
Set ctl = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description & vbNewLine _
& Err.Number, vbOKOnly, "Error"
End Sub
|