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
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