Audit Trail

alm5190

Registered User.
Local time
Today, 04:00
Joined
Mar 23, 2017
Messages
12
I used an audit trail from a previous post to track changes made to a table via a form but I am getting an error when I try to use the second code in the actual form. I get a type mismatch error on the Call line and I don't understand why.

Here is the module:

Code:
Option Compare Database

Const cDQ As String = """"

Sub AuditTrail(frm As Form, recordid As Control)
  'Track changes to data.
  'recordid identifies the pk field's corresponding
  'control in frm, in order to id record.
  Dim ctl As Control
  Dim varBefore As Variant
  Dim varAfter As Variant
  Dim strControlName As String
  Dim strSQL As String
  On Error GoTo ErrHandler
  'Get changed values.
  For Each ctl In frm.Controls
    With ctl
    'Avoid labels and other controls with Value property.
    If .ControlType = acTextBox Then
      If .Value <> .OldValue Then
        varBefore = .OldValue
        varAfter = .Value
        strControlName = .Name
        'Build INSERT INTO statement.
        strSQL = "INSERT INTO " _
           & "Audit (EditDate, User, RecordID, SourceTable, " _
           & " SourceField, BeforeValue, AfterValue) " _
           & "VALUES (Now()," _
           & cDQ & Environ("username") & cDQ & ", " _
           & cDQ & recordid.Value & cDQ & ", " _
           & cDQ & frm.RecordSource & cDQ & ", " _
           & cDQ & .Name & cDQ & ", " _
           & cDQ & varBefore & cDQ & ", " _
           & cDQ & varAfter & cDQ & ")"
        'View evaluated statement in Immediate window.
        Debug.Print strSQL
        DoCmd.SetWarnings False
        DoCmd.RunSQL strSQL
        DoCmd.SetWarnings True
      End If
    End If
    End With
  Next
  Set ctl = Nothing
  Exit Sub

ErrHandler:
  MsgBox Err.Description & vbNewLine _
   & Err.Number, vbOKOnly, "Error"
End Sub

Then I entered this in the "Before Update" section of my form SBAR (Event Procedure). Here is where I get the mismatch error.

Code:
Option Compare Database

Private Sub Form_BeforeUpdate(Cancel As Integer)

  Call basAuditTrail.AuditTrail(Me, SBARNumber)
End Sub



Table1: SBAR
Table2: Audit
Form: SBAR
Module: basAuditTrail
Primary Key: SBARNumber


I am very new to access and just finished my level two class so please bear with me.
 
Last edited:
I think you can see that using the Code Tags (the # toolbar) makes the code much easier to use. :eek:
Code:
Sub AuditTrail(frm As Form, recordid As Control)
    'Track changes to data.
    'recordid identifies the pk field's corresponding
    'control in frm, in order to id record.
    Dim ctl As Control
    Dim varBefore As Variant
    Dim varAfter As Variant
    Dim strControlName As String
    Dim strSQL As String
    On Error GoTo ErrHandler
    'Get changed values.
    For Each ctl In frm.Controls
        With ctl
            'Avoid labels and other controls with Value property.
            If .ControlType = acTextBox Then
                If .Value <> .OldValue Then
                    varBefore = .OldValue
                    varAfter = .Value
                    strControlName = .Name
                    'Build INSERT INTO statement.
                    strSQL = "INSERT INTO " _
                        & "Audit (EditDate, User, RecordID, SourceTable, " _
                        & " SourceField, BeforeValue, AfterValue) " _
                        & "VALUES (Now()," _
                        & cDQ & Environ("username") & cDQ & ", " _
                        & cDQ & recordid.Value & cDQ & ", " _
                        & cDQ & frm.RecordSource & cDQ & ", " _
                        & cDQ & .Name & cDQ & ", " _
                        & cDQ & varBefore & cDQ & ", " _
                        & cDQ & varAfter & cDQ & ")"
                    'View evaluated statement in Immediate window.
                    Debug.Print strSQL
                    DoCmd.SetWarnings False
                    DoCmd.RunSQL strSQL
                    DoCmd.SetWarnings True
                End If
            End If
        End With
    Next
    Set ctl = Nothing
    Exit Sub

ErrHandler:
    MsgBox Err.Description & vbNewLine _
        & Err.Number, vbOKOnly, "Error"
End Sub
 
Yikes I just realized I didn't actually explain the issue I'm having! ugh, rough morning.

I edited the original post.
 
I think you can see that using the Code Tags (the # toolbar) makes the code much easier to use. :eek:
Code:
Sub AuditTrail(frm As Form, recordid As Control)
    'Track changes to data.
    'recordid identifies the pk field's corresponding
    'control in frm, in order to id record.
    Dim ctl As Control
    Dim varBefore As Variant
    Dim varAfter As Variant
    Dim strControlName As String
    Dim strSQL As String
    On Error GoTo ErrHandler
    'Get changed values.
    For Each ctl In frm.Controls
        With ctl
            'Avoid labels and other controls with Value property.
            If .ControlType = acTextBox Then
                If .Value <> .OldValue Then
                    varBefore = .OldValue
                    varAfter = .Value
                    strControlName = .Name
                    'Build INSERT INTO statement.
                    strSQL = "INSERT INTO " _
                        & "Audit (EditDate, User, RecordID, SourceTable, " _
                        & " SourceField, BeforeValue, AfterValue) " _
                        & "VALUES (Now()," _
                        & cDQ & Environ("username") & cDQ & ", " _
                        & cDQ & recordid.Value & cDQ & ", " _
                        & cDQ & frm.RecordSource & cDQ & ", " _
                        & cDQ & .Name & cDQ & ", " _
                        & cDQ & varBefore & cDQ & ", " _
                        & cDQ & varAfter & cDQ & ")"
                    'View evaluated statement in Immediate window.
                    Debug.Print strSQL
                    DoCmd.SetWarnings False
                    DoCmd.RunSQL strSQL
                    DoCmd.SetWarnings True
                End If
            End If
        End With
    Next
    Set ctl = Nothing
    Exit Sub

ErrHandler:
    MsgBox Err.Description & vbNewLine _
        & Err.Number, vbOKOnly, "Error"
End Sub


Sorry! I corrected the original post. Thanks!
 
Try: Call basAuditTrail.AuditTrail(Me, Me.SBARNumber)
 
Are you sure the SBARNumber control in named "SBARNumber"?
 
If you zip up your db and post it we won't have to play these guessing games. Remove any sensitive data of course.
 
It's approved now and I'm looking at it. Sorry about the delay.
 
The name of the Control on your form that displayed the SBARNumber is Text31. For the code to work it needs to be named SBARNumber. Change it and your code starts to work.
 
Last edited:
Wow do I feel dumb for missing that!

Thanks though! I really appreciate the help!
 
Sorry one more thing

So it isn't working for all fields. For the timeline assessment, and recommendation fields it is recording an edit date, user, record ID, source table and field but nothing for the before and after value. Not sure why?
 

Users who are viewing this thread

Back
Top Bottom