troubleshooting Question

MS Access Trail

Avatar of shieldsco
shieldscoFlag for United States of America asked on
Microsoft Access
3 Comments1 Solution85 ViewsLast Modified:
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

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


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

Main Form

Private Sub Form_AfterDelConfirm(Status As Integer)

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


End Sub

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

End Sub
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros