Results 1 to 6 of 6
  1. #1
    ryanmce92 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    48

    Post Having an audit trail

    Hi Folks,

    After much trekking on the internet I have managed to find an audit trail code which works perfect with my database. However I have been having one slight issue so if anyone could help me I would greatly appreciate it.



    I have the below code which works for text boxes but for some reason when I use check boxes or combo boxes it does not seem to track these changes, if any of you could point me in the right direction that would be great!.

    Code:
    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
    Please bare with me if I have a question or two kind of a newbie at this sort of thing so any help would be greatly appreciated!!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    I cycle thru looking for my objects using TYPENAME...

    Code:
    Dim ctl
    For Each ctl In Controls
      Select Case TypeName(ctl)
        Case "CommandButton"
           MsgBox ctl.Name, , "button"
           
        Case "ComboBox"
          MsgBox ctl.Name, , "combo"
      End Select
    Next

  3. #3
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The right direction is:

    If .ControlType = acTextBox Then

    which limits the code to textboxes. You can use ranman's method or add to the above with OR.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    ryanmce92 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    48
    Quote Originally Posted by pbaldy View Post
    The right direction is:

    If .ControlType = acTextBox Then

    which limits the code to textboxes. You can use ranman's method or add to the above with OR.
    How would I add to the above with OR ?

    I have this solution working now and its exactly what im looking for so if I could get that working in any way it would be perfect.

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can start with this line of code
    Code:
    If .ControlType = acTextBox Then
    And determine which constants to use
    https://msdn.microsoft.com/en-us/lib...ffice.11).aspx

    I think that list should work combos and such for more current versions of Access. Maybe use the OR operator to include additional control types.

    something like
    If .ControlType = acTextBox or .ControlType = acComboBox Then

  6. #6
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by ryanmce92 View Post
    How would I add to the above with OR ?
    Like ItsMe showed.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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 Trail
    By bigroo in forum Programming
    Replies: 4
    Last Post: 02-22-2012, 07:55 PM
  3. audit trail
    By slimjen in forum Forms
    Replies: 1
    Last Post: 10-13-2011, 01:52 PM
  4. Audit trail question #2
    By jpkeller55 in forum Access
    Replies: 9
    Last Post: 09-13-2010, 05:55 PM
  5. Audit trail question
    By jpkeller55 in forum Access
    Replies: 5
    Last Post: 09-13-2010, 04:12 PM

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