Access 2010 Audit Trail on SubForms

前端 未结 3 1645
梦如初夏
梦如初夏 2021-01-16 03:45

I am having trouble getting the code I found for an audit trail to work with sub forms. The origninal code is from http://www.fontstuff.com/access/acctut21.htm. I would rath

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-16 04:28

    I actually have a much simpler solution. You need to pass the (sub)form object along to the main basAudit sub.

    Now, becuase the subform is the one initiating the command, it will be passed along to basAudit sub instead of the ActiveForm (wich is the main form, not the subform).

    Modify the basAudit module as followed:

    Sub AuditChanges(IDField As String, UserAction As String, UsedForm As Form)
        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 UsedForm.Controls
                    If ctl.Tag = "Audit" Then
                        If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
                            With rst
                                .AddNew
                                ![DateTime] = datTimeCheck
                                ![UserName] = strUserID
                                ![FormName] = UsedForm.Name
                                ![Action] = UserAction
                                ![RecordID] = UsedForm.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] = UsedForm.Name
                    ![Action] = UserAction
                    ![RecordID] = UsedForm.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
    

    Change the AfterDelConfirm sub as followed:

    Private Sub Form_AfterDelConfirm(Status As Integer)
        If Status = acDeleteOK Then Call AuditChanges("Site", "DELETE", Form)
    End Sub
    

    And last, change the BeforeUpdate sub as followed:

    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Me.NewRecord Then
            Call AuditChanges("Site", "NEW", Form)
        Else
            Call AuditChanges("Site", "EDIT", Form)
        End If
    End Sub
    

提交回复
热议问题