Setup Audit Trail in Access 2013

squeakums

Well-known Member
Joined
May 15, 2007
Messages
839
Office Version
  1. 365
How can you setup an audit trail in Access 2013? I found this code online, but I cannot seem to get it to work:

Function WriteChanges()

Dim f As Form
Dim c As Control
Dim frm As String
Dim user As String
Dim sql As String
Dim changes As String
Dim db As DAO.Database

Set f = Screen.ActiveForm
Set db = CurrentDb

frm = Screen.ActiveForm.Name
user = Application.CurrentUser
changes = ""

sql = "INSERT INTO AuditTrail " & _
"([FormName], [User], [ChangesMade]) " & _
"VALUES ('" & frm & "', '" & user & "', "

For Each c In f.Controls

Select Case c.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup
If IsNull(c.OldValue) And Not IsNull(c.Value) Then
changes = changes & _
c.Name & "--" & "BLANK" & "--" & c.Value & _
vbCrLf
ElseIf IsNull(c.Value) And Not IsNull(c.OldValue) Then
changes = changes & _
c.Name & "--" & c.OldValue & "--" & "BLANK" & _
vbCrLf
ElseIf c.Value <> c.OldValue Then
changes = changes & _
c.Name & "--" & c.OldValue & "--" & c.Value & _
vbCrLf
End If
End Select

Next c

sql = sql & "'" & changes & "');"

db.Execute sql, dbFailOnError

Set f = Nothing
Set db = Nothing

End Function
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I use this, usage:
Post2Audit "tEmployees","Hrs",0,8

Code:
Public Sub Post2Audit(pvTable, pvField, pvOldVal, pvNewVal)
Dim sSql As String
vUser = Environ("Username")
sSql = "INSERT INTO tAuditTrail ([Table],[Field],[OldVal],[NewVal],[USER],[EntryDate]) values ('" & pvTable & "','" & pvField & "','" & pvOldVal & "','" & pvNewVal & "','" & vUser & "',#" & Now() & "#)"
'Debug.Print sSql
DoCmd.SetWarnings False
DoCmd.RunSQL sSql
DoCmd.SetWarnings True
End Sub
[/code
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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