I have been adding this audit trail creaated by ghudson
on Access Wrold Forum to my application with some of the modifications suggested by others on that. The audit trail works great on one form (includes subforms) but I receive a compile error when I try to add the functionality to another form (no subforms).
I receive an error: Compile Error Invalid use of property on the Call AuditTrail(Me) line. I read the help for this error but I have no clue as what it means or how to fix it. Can anyone help me figure out why the same Call statement works fine on one form but not another?
I have included the module code for the function below:
on Access Wrold Forum to my application with some of the modifications suggested by others on that. The audit trail works great on one form (includes subforms) but I receive a compile error when I try to add the functionality to another form (no subforms).
I receive an error: Compile Error Invalid use of property on the Call AuditTrail(Me) line. I read the help for this error but I have no clue as what it means or how to fix it. Can anyone help me figure out why the same Call statement works fine on one form but not another?
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If [txtProjectEndDT] < [txtProjectBeginDT] Then
MsgBox "Project End Date Cannot Be Less Than Project Begin Date"
txtProjectEndDT.SetFocus
Cancel = True
End If
DoEvents
Call AuditTrail(Me)
End Sub
I have included the module code for the function below:
Code:
Option Compare Database
Option Explicit
Public Function AuditTrail(MyForm As Form)
On Error GoTo Err_Audit_Trail
'ACC2000: How to Create an Audit Trail of Record Changes in a Form
'[URL unfurl="true"]http://support.microsoft.com/default.aspx?scid=kb;en-us;197592[/URL]
Dim ctl As Control
Dim sUser As String
Dim strUserName As String
'get the login user name
strUserName = Environ("USERNAME")
sUser = strUserName
'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 " & sUser & ";"
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 " & sUser & ";"
'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.
If Err.Number = 3251 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