Results 1 to 4 of 4
  1. #1
    rebeccab is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2014
    Posts
    4

    AuditTrail not working on Sub Forms

    Hiya,

    I use the following module to run an audit trail on my database, which is then linked on to the form by using the following code on the BeforeUpdate property of the form. This works perfectly on the form itself - and in tabs - until I am using a subform. How can I get the module to work without having to name each subform (this form uses 3 SFs!)??

    Code:
    Sub PropertyAuditChanges(IDField As String)
        On Error GoTo PropertyAuditChanges_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 PropertyAudit", cnn, adOpenDynamic, adLockOptimistic
        datTimeCheck = Now()
        strUserID = Environ("USERNAME")
        For Each ctl In Screen.ActiveForm.Controls
            If ctl.Tag = "PropertyAudit" 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
                        .Update
                    End With
                End If
            End If
        Next ctl
    PropertyAuditChanges_Exit:
        On Error Resume Next
        rst.Close
        cnn.Close
        Set rst = Nothing
        Set cnn = Nothing
        Exit Sub
    PropertyAuditChanges_Err:
        MsgBox Err.Description, vbCritical, "ERROR!"
        Resume PropertyAuditChanges_Exit
    End Sub
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Not Me.NewRecord Then Call PropertyAuditChanges("SiteCode")
    End Sub


  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I suspect you'll have to call the function from each of the subforms. If you call from the main form's before update event, the subform's will already be updated and the OldValue property would not be available.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rebeccab is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2014
    Posts
    4
    Thanks for your reply Paul. I have already tried calling the function on the BeforeUpdate of each subform, but I can't seem to make it work

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I don't know what "can't seem to make it work" means exactly. Do you get an error? It could be that ActiveForm will always refer to the main form, so you may need to modify the function to accept the name of the form or subform, and use that instead.

    By the way, opening the recordset on the entire table:

    Code:
    rst.Open "SELECT * FROM PropertyAudit", cnn, adOpenDynamic, adLockOptimistic
    could prove to be a performance problem when the table accumulates a lot of records. You can use the append only option or:

    Code:
    rst.Open "SELECT * FROM PropertyAudit WHERE 1=0", cnn, adOpenDynamic, adLockOptimistic
    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: 1
    Last Post: 07-18-2014, 11:32 AM
  2. AuditTrail Tracking Problem, Access 2010
    By accote in forum Access
    Replies: 31
    Last Post: 08-23-2013, 04:05 PM
  3. Replies: 3
    Last Post: 08-19-2013, 01:23 PM
  4. Applying AuditTrail to subform on a form
    By jle0003 in forum Access
    Replies: 2
    Last Post: 09-28-2012, 04:59 PM
  5. Forms Order By not working
    By cowboy in forum Forms
    Replies: 3
    Last Post: 04-21-2010, 10:29 AM

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