Hi, all -
I found code online for creating an Access audit trail which seems pretty good (conceptually) but I am having a basic problem: the code isn't recognizing data edits.
First of all, I am running a 2003 database through Access 2007 (which may be becoming 2013 soon).
The code (at least the first part where the trouble is) is:
Sub AuditChanges(IDField As String, UserAction As String)
On Error GoTo AuditChanges_Err
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim ctl As Control
Dim datTimeCheck As Date
Dim strUserID As String
Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM tblAuditTrail", cnn, adOpenDynamic, adLockOptimistic
datTimeCheck = Now()
strUserID = CurrentUser
Debug.Print strUserID
Select Case UserAction
Case "EDIT"
For Each ctl In Screen.ActiveForm.Controls
If ctl.Tag = "Audit" Then
If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
Debug.Print "same to them"
With rst
.AddNew
![DateTime] = datTimeCheck
![UserName] = strUserID
![FormName] = Screen.ActiveForm.Name
![Action] = UserAction
![recordid] = Screen.ActiveForm.Controls(IDField).Value
![FieldName] = ctl.ControlSource
![OldValue] = ctl.OldValue
![NewValue] = ctl.Value
.Update
End With
End If
End If
The problem is with the "If nz(ctlvalue) <> nz(ctl.oldvalue) line.
I did a debug.print test and it doesn't recognize the changes in the data.
Any help will be appreciated.
Paul
I found code online for creating an Access audit trail which seems pretty good (conceptually) but I am having a basic problem: the code isn't recognizing data edits.
First of all, I am running a 2003 database through Access 2007 (which may be becoming 2013 soon).
The code (at least the first part where the trouble is) is:
Sub AuditChanges(IDField As String, UserAction As String)
On Error GoTo AuditChanges_Err
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim ctl As Control
Dim datTimeCheck As Date
Dim strUserID As String
Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM tblAuditTrail", cnn, adOpenDynamic, adLockOptimistic
datTimeCheck = Now()
strUserID = CurrentUser
Debug.Print strUserID
Select Case UserAction
Case "EDIT"
For Each ctl In Screen.ActiveForm.Controls
If ctl.Tag = "Audit" Then
If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
Debug.Print "same to them"
With rst
.AddNew
![DateTime] = datTimeCheck
![UserName] = strUserID
![FormName] = Screen.ActiveForm.Name
![Action] = UserAction
![recordid] = Screen.ActiveForm.Controls(IDField).Value
![FieldName] = ctl.ControlSource
![OldValue] = ctl.OldValue
![NewValue] = ctl.Value
.Update
End With
End If
End If
The problem is with the "If nz(ctlvalue) <> nz(ctl.oldvalue) line.
I did a debug.print test and it doesn't recognize the changes in the data.
Any help will be appreciated.
Paul