PDA

View Full Version : Change Log Help - nearly there



TimT
05-06-2016, 09:20 AM
Hello!

I am building a change log/audit trail in Access (in other words, a form, when updated, creates new entries in a table to track the changes). The execution of this is currently handled Before Update, and the module is shown below:


Sub AuditChanges(IDField As String)
On Error GoTo AuditChanges_Err
DoCmd.Close acTable, "tblAuditTrail", acSaveYes
DoCmd.Close acTable, "Master Route Plan List", acSaveYes
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")
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
![recordid] = Screen.ActiveForm.Controls(IDField).Value
![FieldName] = ctl.ControlSource
![OldValue] = ctl.OldValue
![NewValue] = ctl.Value
![Region] = Screen.ActiveForm.Controls!Region.Value
![CAR] = Screen.ActiveForm.Controls!CAR.Value
![Reason] = Screen.ActiveForm.Controls!Reason.Value
.Update
End With
End If
End If
Next ctl
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


The last generated column, "Reason", is what's giving me issues. Right now, it uses the value that was in the record before, but I need it to reflect the new, user generated value inserted into the text box.

I have extremely limited VBA knowledge, so my apologies if this an overly basic question. I am on Access 2013 and Windows 7.

jonh
05-10-2016, 07:35 AM
Try removing .value