A family of Microsoft relational database management systems designed for ease of use.
You would change the first parameter to a text string of the table name that field is from.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello, i know this is very common question however, since i am not an expert, i could not make it work after trying lots of ways suggested in some websites. I am using Martin Green's code. link here http://www.fontstuff.com/access/acctut21.htm .
below is the code and everything is as described on article.
Sub AuditChanges(IDField As String, UserAction As String)
On Error GoTo AuditChanges_Err
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim ctl As Control
Dim datTimeCheck As Date
Dim strUserID As String
Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM tblAuditTrail", cnn, adOpenDynamic, adLockOptimistic
datTimeCheck = Now()
strUserID = Environ("USERNAME")
Select Case UserAction
Case "EDIT"
For Each ctl In Screen.ActiveForm.Controls
If ctl.Tag = "Audit" Then
If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
With rst
.AddNew
![DateTime] = datTimeCheck
![UserName] = strUserID
![FormName] = Screen.ActiveForm.Name
![Action] = UserAction
![recordID] = Screen.ActiveForm.Controls(IDField).Value
![FieldName] = ctl.ControlSource
![OldValue] = ctl.OldValue
![NewValue] = ctl.Value
.Update
End With
End If
End If
Next ctl
Case Else
With rst
.AddNew
![DateTime] = datTimeCheck
![UserName] = strUserID
![FormName] = Screen.ActiveForm.Name
![Action] = UserAction
![recordID] = Screen.ActiveForm.Controls(IDField).Value
.Update
End With
End Select
AuditChanges_Exit:
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
AuditChanges_Err:
MsgBox Err.Description, vbCritical, "ERROR!"
Resume AuditChanges_Exit
End Sub
I am sure it needs minor code adjustments to read the changes on subforms. Any help would be appreciated.
A family of Microsoft relational database management systems designed for ease of use.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
You would change the first parameter to a text string of the table name that field is from.
Thank you..
Last thing... if form is bound to a query what should i change? Do i need to have PK field on form itself? Or if it is in query, will it work?
Thank you Scott.
I have 3 questions.
1- Will it work on subform?
2- If i want strUser as current Computer log-in user what is the code.?
3- I have to place before update event of every control instead of form right?
I prefer to do my audits on the Control Level. The system I use has the following AutditTrails function:
Public Sub AuditTrail(strTable As String, strField As String, lngRecord As Long, varOld As Variant, varNew As Variant)
Dim strSQL As String, strUser As String
strUser = DLookup("[EmpID]", "tblParameters")
strSQL = "INSERT INTO tblAuditTrail (Tablename, Fieldname, RecordID, OldValue, NewValue, ChangeDate, ChangeBy) " & _
"VALUES('" & strTable & "', '" & strField & "', " & lngRecord & ", '" & varOld & "', '" & varNew & "', #" & _
Now() & "#, '" & strUser & "');"
CurrentDb.Execute strSQL, dbFailOnError
End Sub
You can replace the strUser line with however you identify the current user.
I call this function in the Before Update event of any control I want to Audit:
Call AuditTrail(Screen.ActiveForm.RecordSource, Screen.ActiveControl.ControlSource, Me.PrimaryKey, Screen.ActiveControl.OldValue, Screen.ActiveControl)
The only thing that needs to be changed is the name of the control bound to your primary key. However, Screen.ActiveForm.Recordsource MAY need to be changed if the recordsource is not a table.
I also have an Audit Trail that works on the Table level, described in my blog on Audit Trails with Data macros.