Link to home
Start Free TrialLog in
Avatar of shieldsco
shieldscoFlag for United States of America

asked on

MS Access Trail

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

ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shieldsco

ASKER

Thanks
You're welcome.