We help IT Professionals succeed at work.

MS Access Trail

shieldsco
shieldsco asked
on
79 Views
Last Modified: 2019-03-25
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

CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
You're welcome.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.