Hiya,
I use the following module to run an audit trail on my database, which is then linked on to the form by using the following code on the BeforeUpdate property of the form. This works perfectly on the form itself - and in tabs - until I am using a subform. How can I get the module to work without having to name each subform (this form uses 3 SFs!)??
Code:
Sub PropertyAuditChanges(IDField As String)
On Error GoTo PropertyAuditChanges_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 PropertyAudit", cnn, adOpenDynamic, adLockOptimistic
datTimeCheck = Now()
strUserID = Environ("USERNAME")
For Each ctl In Screen.ActiveForm.Controls
If ctl.Tag = "PropertyAudit" Then
If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
With rst
.AddNew
![DateTime] = datTimeCheck
![UserName] = strUserID
![FormName] = Screen.ActiveForm.Name
![RecordID] = Screen.ActiveForm.Controls(IDField).Value
![FieldName] = ctl.ControlSource
![OldValue] = ctl.OldValue
![NewValue] = ctl.Value
.Update
End With
End If
End If
Next ctl
PropertyAuditChanges_Exit:
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
PropertyAuditChanges_Err:
MsgBox Err.Description, vbCritical, "ERROR!"
Resume PropertyAuditChanges_Exit
End Sub
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not Me.NewRecord Then Call PropertyAuditChanges("SiteCode")
End Sub