Results 1 to 4 of 4
  1. #1
    RosaCat is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2019
    Posts
    22

    Unhappy Audit VBA not working

    Hello,

    I have issues with the Audit table. Here's the Module VBA:
    Option Compare Database


    Const cDQ As String = """"
    Sub AuditTrail(frm As Form, recordid As Control) <-----------------Debug Highlights this
    '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 '<--------------------- Debug makes this statement font red.
    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

    This is what I entered into the form "Before Update":

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    Call AuditTrail(Me, ContactID)
    If MsgBox("Do you want to save the changes?", vbYesNo) = vbNo Then
    Me.Undo
    Cancel = True
    End If
    End Sub

    I don't know VBA well enough to figure this one out. Please Help!

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    Should probably be
    Code:
    If .Value <> .OldValue Then

  3. #3
    RosaCat is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2019
    Posts
    22
    Thank you for being so smart!!!

    Have a Great Day!

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    Happy to help!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Audit Trail - Not Working
    By Delfina in forum Programming
    Replies: 12
    Last Post: 02-06-2019, 08:09 PM
  2. Access 2010: Audit Trail not Working in Subform
    By besuchanko in forum Programming
    Replies: 19
    Last Post: 11-12-2015, 03:47 AM
  3. Replies: 2
    Last Post: 02-09-2013, 12:39 PM
  4. Audit Trail for SubForms Not Working
    By PinkNinja in forum Access
    Replies: 6
    Last Post: 01-07-2011, 11:03 PM
  5. Audit trail not working on form with subform
    By jpkeller55 in forum Access
    Replies: 52
    Last Post: 01-07-2011, 12:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums