I have a procedure I want to run each time a control box is updated, but the code I have is a For Each because it was made to run only once at the Form's After Update event. But when I do that, it doesn't pick up the change.
Anyone know how to properly remove a For Each - while still capturing the correct control being updated?
Anyone know how to properly remove a For Each - while still capturing the correct control being updated?
Code:
Sub AuditTrail(frm As Form, recordid As Control)
Dim ctl As Control
Dim varBefore As Variant
Dim varAfter As Variant
Dim strControlName, msg As String
Dim strSQL As String
'On Error GoTo ErrHandler
For Each ctl In frm.Controls ''''''''This is what I want to remove,
''''''but still have it capture the modified control
With ctl
Select Case .ControlType
Case acCheckBox, acComboBox, acListBox, acOptionButton, acOptionGroup, acTextBox
If (.Value <> .OldValue) Or ((Not IsNull(.OldValue) _
And IsNull(.Value))) Or ((IsNull(.OldValue) And Not IsNull(.Value))) Then
varBefore = .OldValue
varAfter = .Value
strControlName = .Name
strSQL = "INSERT INTO " _
& "Audit (EditDate, User, RecordID, SourceTable, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (Now()," _
& cDQ & Environ("username") & cDQ & ", " _
& cDQ & recordid.Value & cDQ & ", " _
& cDQ & frm.RecordSource & cDQ & ", " _
& cDQ & .Name & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
Debug.Print strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End If
End Select
End With
Next
Set ctl = Nothing
Exit Sub
'ErrHandler:
'MsgBox Err.Description & vbNewLine _
'& Err.Number, vbOKOnly, "Error"
End Sub