What's wrong with my Audit Trail code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flower88
    New Member
    • Jun 2010
    • 22

    What's wrong with my Audit Trail code?

    I have this code to keep track of the changes in a form. It is suppose to tell me what was changed, when, and by whom. I have this code as a module

    Code:
    Public Function Audit_Trail()
    On Error GoTo Err_Audit_Trail
        
        
        Dim MyForm As Form
        Dim ctl As Control
        Dim sUser As String
        Set MyForm = Screen.ActiveForm
        
        'If new record, record it in audit trail and exit function.
        If MyForm.NewRecord = True Then
            MyForm!AuditTrail = MyForm!tbAuditTrail & "New Record added on " & Now & " by " & UserName() & ";"
            Exit Function
        End If
        
        'Set date and current user if the form (current record) has been modified.
        MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf & vbLf & "Changes made on " & Now & " by " & UserName() & ";"
        
        'Check each data entry control for change and record old value of the control.
        For Each ctl In MyForm.Controls
        
        'Only check data entry type controls.
        Select Case ctl.ControlType
        Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox
            If ctl.Name = "tbAuditTrail" Then GoTo TryNextControl 'Skip AuditTrail field.
                'If new and old value do not equal
                If ctl.Value <> ctl.OldValue Then
                    MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf & ctl.Name & ": Changed From: " & ctl.OldValue & ", To: " & ctl.Value
                'If old value is Null and new value is not Null
                ElseIf IsNull(ctl.OldValue) And Len(ctl.Value) > 0 Or ctl.OldValue = "" And Len(ctl.Value) > 0 Then
                    MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf & ctl.Name & ": Was Previoulsy Null, New Value: " & ctl.Value
                'If new value is Null and old value is not Null
                ElseIf IsNull(ctl.Value) And Len(ctl.OldValue) > 0 Or ctl.Value = "" And Len(ctl.OldValue) > 0 Then
                    MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf & ctl.Name & ": Changed From: " & ctl.OldValue & ", To: Null"
                End If
        End Select
        
    TryNextControl:
        Next ctl
        
    Exit_Audit_Trail:
        Exit Function
        
    Err_Audit_Trail:
        If Err.Number = 64535 Then 'Operation is not supported for this type of object.
            Exit Function
        ElseIf Err.Number = 2475 Then 'You entered an expression that requires a form to be the active window
            Beep
            MsgBox "A form is required to be the active window!", vbCritical, "Invalid Active Window"
        Else
            Beep
            MsgBox Err.Number & " - " & Err.DESCRIPTION
        End If
        Resume Exit_Audit_Trail
        
    End Function
    and I'm calling the module in my form under before update like this:

    Code:
    On Error GoTo Form_BeforeUpdate_Err
        
        Call Audit_Trail
        
    Form_BeforeUpdate_Exit:
        Exit Sub
        
    Form_BeforeUpdate_Err:
        MsgBox Err.Number & " - " & Err.DESCRIPTION
        Resume Form_BeforeUpdate_Exit
    Right now, it tells me when the record was added and by whom but if I go back and edit the record then it does nothing.

    Any ideas?

    Thanks for the help!
Working...