MS Access Trail

shieldsco
shieldsco used Ask the Experts™
on
I'm using the following code to track changes in an Access db. There are two Pages main form and sub form (Profile Card Status and Travel Document). The code works as expected when I add new records.... the tblAuditTrail is updated with the recordID and action ("New") for both the main form and sub form , however when I delete a record the recordID is not recorded for the main form in tblAuditTrail. The action is recorded correctly "Deleted". The sub form is not updated for the recordID and action ("Deleted). Thanks


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

Open in new window


Main Form:
Private Sub Form_BeforeUpdate(Cancel As Integer)


If Me.NewRecord Then
    'Calls modAudit function to record new records to Audit Trail
    Call AuditChanges(Me, "PID", "NEW")

Else
    'Calls modAudit function to record edits to Audit Trail
    Call AuditChanges(Me, "PID", "EDIT")

End If

End Sub

Open in new window



Subform:
Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.NewRecord Then
    'Calls modAuditSub function to record new data to Audit Trail
    Call AuditChanges(Me, "TID", "NEW") 'original

Else
    'Calls modAuditSub function to record edits to Audit Trail
    Call AuditChanges(Me, "TID", "EDIT") 'original

End If

End Sub

Open in new window


Main Form

Private Sub Form_AfterDelConfirm(Status As Integer)

If Status = acDeleteOK Then Call AuditChanges(Me, "PID", "DELETE")


End Sub

Open in new window


Subform
Private Sub Form_AfterDelConfirm(Status As Integer)
If Status = acDeleteOK Then Call AuditChanges(Me, "TID", "DELETE")

End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017
Commented:
By the time the AfterDelConfirm runs, the record has already been deleted so the ID is gone.  You need to move the code to an earlier event.

Regarding the subform events, they will not fire if CascadeDelete is what is causing the subform records to be deleted.  You would need to force the user to delete the subform items one at a time OR you would have create a code loop that deletes the subform records one at a time so you can capture their ID and save the audit trail record.

Author

Commented:
Thanks
Distinguished Expert 2017

Commented:
You're welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial