Hello,
I have been trying to get my audit trail function to work for a week now and it's not doing anything. I don't get an error message, it's just not adding the changes to the AuditTrail table.
I created the function module as shown below:
I then created the code in my Data Entry Form under the Before Update event to add any new or edited data into the audit trail table.
I also added the following code in the After Delete Confirm event to keep track of deleted records in the audit trail.
Am I missing something with the audit trail?
I have the AuditTrail table, I have the audit function, and I added the code to the Data Entry Form where I want the audit trail to occur.
I am not sure if this might help, but I do have buttons in my form to add, delete and edit records. But each button has a different code to add, delete, edit records in the MasterThrougput table.
I would appreciate some help if possible.
Thank you,
Miriam
I have been trying to get my audit trail function to work for a week now and it's not doing anything. I don't get an error message, it's just not adding the changes to the AuditTrail table.
I created the function module as shown below:
Code:
Option Compare Database
Option Explicit
Public Function AuditChanges(RecordID As String, UserAction As String)
On Error GoTo AuditChanges_Err
Dim DB As Database
Dim rst As Recordset
Dim clt As Control
Dim UserLogin As String
Set DB = CurrentDb
Set rst = DB.OpenRecordset("select * from AuditTrail", adOpenDynamic)
UserLogin = Environ("USERNAME")
Select Case UserAction
Case "New"
With rst
.AddNew
![DateTime] = Now()
!UserName = UserLogin
!FormName = Screen.ActiveForm.Name
!RecordID = Screen.ActiveForm.Controls(RecordID).Value
!Action = UserAction
.Update
End With
Case "Delete"
With rst
.AddNew
![DateTime] = Now()
!UserName = UserLogin
!FormName = Screen.ActiveForm.Name
!RecordID = Screen.ActiveForm.Controls(RecordID).Value
!Action = UserAction
.Update
End With
Case "Edit"
For Each clt In Screen.ActiveForm.Controls
If (clt.ControlType = acTextBox _
Or clt.ControlType = acComboBox) Then
If Nz(clt.Value) <> Nz(clt.OldValue) Then
With rst
.AddNew
![DateTime] = Now()
!UserName = UserLogin
!FormName = Screen.ActiveForm.Name
!RecordID = Screen.ActiveForm.Controls(RecordID).Value
!Action = UserAction
!FieldName = clt.ControlSource
!OldValue = clt.OldValue
!NewValue = clt.Value
.Update
End With
End If
End If
Next clt
End Select
rst.Close
DB.Close
Set rst = Nothing
Set DB = Nothing
AuditChanges_Err:
MsgBox Err.Number & " : " & Err.Description, vbCritical, "ERROR!"
Exit Function
End Function
I then created the code in my Data Entry Form under the Before Update event to add any new or edited data into the audit trail table.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
Call AuditChanges("CaseNo", "New")
Else
Call AuditChanges("CaseNo", "Edit")
End If
End Sub
I also added the following code in the After Delete Confirm event to keep track of deleted records in the audit trail.
Code:
Private Sub Form_AfterDelConfirm(Status As Integer)
If Status = acDeleteOK Then Call AuditChanges("CaseNo", "DELETE")
End Sub
Am I missing something with the audit trail?
I have the AuditTrail table, I have the audit function, and I added the code to the Data Entry Form where I want the audit trail to occur.
I am not sure if this might help, but I do have buttons in my form to add, delete and edit records. But each button has a different code to add, delete, edit records in the MasterThrougput table.
I would appreciate some help if possible.
Thank you,
Miriam