Access Audit Trail - Need to only record deleted and edited records

MHamid

Active Member
Joined
Jan 31, 2013
Messages
472
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I am having an issue with my audit trail. It works perfectly, however I was told that only edited and deleted records will need to be kept in the audit trail. New records do not need to be in the audit trail.

As I attempted to manipulate my code to exclude new records, I've noticed that it still records them as Edited records. This should not be happening. If it's a new record being added, then the audit trail should ignore these. What is it that I am doing wrong?

Here is the Audit Trail Function code:
Code:
Option Compare Database
Public Function AuditChanges(txtRefID As String, UserAction As String)
On Error GoTo AuditErr
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 tbl_AuditTrail", adOpenDynamic)
UserLogIn = Environ("USERNAME")
Select Case UserAction
    Case "New"
        With rst
            .AddNew
            ![DateTime] = Now()
            ![UserName] = UserLogIn
            ![FormName] = Screen.ActiveForm.Name
            ![Action] = UserAction
            ![RecordID] = Screen.ActiveForm.Controls(txtRefID).Value
            .Update
        End With
        
    Case "Delete"
        With rst
            .AddNew
            ![DateTime] = Now()
            ![UserName] = UserLogIn
            ![FormName] = Screen.ActiveForm.Name
            ![Action] = UserAction
            ![RecordID] = Screen.ActiveForm.Controls(txtRefID).Value
            .Update
        End With
        
    Case "Edit"
        For Each clt In Screen.ActiveForm.Controls
            If (clt.ControlType = acComboBox _
                 Or clt.ControlType = acTextBox) Then
                 If Nz(clt.Value) <> Nz(clt.OldValue) Then
                    With rst
                        .AddNew
                        ![DateTime] = Now()
                        ![UserName] = UserLogIn
                        ![FormName] = Screen.ActiveForm.Name
                        ![Action] = UserAction
                        ![RecordID] = Screen.ActiveForm.Controls(txtRefID).Value
                        ![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
AuditErr:
    'MsgBox Err.Number & " : " & Err.Description, vbCritical, "Error"
    Exit Function
    
End Function


The code I originally used to call the audit trail for all new and edited records is:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
    Call AuditChanges("txtRefID", "New")
Else
    Call AuditChanges("txtRefID", "Edit")
End If
End Sub

What is it that I need to do to exclude new records?
I was thinking that the "Call AuditChanges("txtRefID", "New")" will need to be changed to do nothing if it's a new record, but I don't know what it should be replaced with.


Thank you
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
As a start, since you want to do nothing, you can try replacing with nothing:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
    'Call AuditChanges("txtRefID", "New")
Else
    Call AuditChanges("txtRefID", "Edit")
End If
End Sub

or (same logical result):
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not Me.NewRecord Then
    Call AuditChanges("txtRefID", "Edit")
End If
End Sub
 
Upvote 0
Ha! That simple. As far as I can tell it works. Thank you Xenou!!

However, I added a date and an attachment columns to my table and form and now the add button is not working again. I really think I'm going to completely change the add record code. Ugh!!!
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,085
Members
449,064
Latest member
MattDRT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top