Results 1 to 2 of 2
  1. #1
    Chatholo is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2013
    Posts
    34

    Audit Log Macro

    Hi!

    I was asked to build a log table im my database to keep track of user/date/action/old data/new data. I have tried a few ways to do it but can't seem to be entirely successful.

    I am very new to VBA and would like some help to understand how tro apply this macro.
    In the sample database attached I am using the Basaudit macro from Martin Green.

    Here is the code:


    Option Compare Database
    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

    Can ayone tell me why it's not working and how I can fix it.

    Thank you in advance for your help!
    C.
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Why isn't it working, what happens - error message, wrong results, nothing?

    Review link at bottom of my post for debug guidelines.

    BTW, that is not a macro, it is a VBA procedure.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 2
    Last Post: 02-09-2013, 12:39 PM
  2. Audit Log
    By Darkladymelz in forum Programming
    Replies: 15
    Last Post: 03-06-2012, 11:36 AM
  3. Audit Trail
    By bigroo in forum Programming
    Replies: 4
    Last Post: 02-22-2012, 07:55 PM
  4. audit trail
    By slimjen in forum Forms
    Replies: 1
    Last Post: 10-13-2011, 01:52 PM
  5. audit Log
    By mwabbe in forum Access
    Replies: 42
    Last Post: 05-30-2011, 07:23 AM

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