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 SelectAuditChanges_Exit: On Error Resume Next rst.Close cnn.Close Set rst = Nothing Set cnn = Nothing Exit SubAuditChanges_Err: MsgBox Err.Description, vbCritical, "ERROR!" Resume AuditChanges_ExitEnd Sub
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 IfEnd Sub
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") 'originalElse 'Calls modAuditSub function to record edits to Audit Trail Call AuditChanges(Me, "TID", "EDIT") 'originalEnd IfEnd Sub