
Maintaining the integrity of your database is essential, especially when multiple users access and modify the data. An audit log in MS Access helps track changes, providing transparency and accountability by recording who made changes, when, and what was changed. This tutorial will guide you through creating an audit log step by step.
An audit log, also known as a change log or history table, is a system that records changes made to a database. It captures:
Create a table in MS Access to store the audit log information.
Set up a form for users to enter data.
To track changes, use VBA to log the changes into `tblAuditLog`.
Here’s how to do it:
Public Sub AuditChanges(tblName As String, fldName As String, oldVal As Variant, newVal As Variant)
On Error Resume Next
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("tblAuditLog")
rst.AddNew
rst!ChangeDate = Now()
rst!UserName = Environ("UserName") ' Captures the current user's Windows login name
rst!TableName = tblName
rst!FieldName = fldName
rst!OldValue = oldVal
rst!NewValue = newVal
rst.Update
rst.Close
Set rst = Nothing
Set db = Nothing
End SubTo log changes, attach the `AuditChanges` procedure to your form’s BeforeUpdate event.
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If Nz(ctl.OldValue, "") <> Nz(ctl.Value, "") Then
Call AuditChanges(Me.RecordSource, ctl.Name, ctl.OldValue, ctl.Value)
End If
End If
Next ctl
End SubBy implementing an audit log in MS Access, you can track changes effectively, ensuring data integrity and security. The combination of a well-structured table, VBA automation, and proper testing makes this approach robust and practical for real-world applications.
Ready to streamline your business with our MS Access Custom Database Services? Don’t let outdated or inefficient systems hold you back. Contact us today to discuss your requirements and receive a free consultation.
Also Read: