fritz.panganiban
Registered User.
- Local time
 - Yesterday, 20:29
 
- Joined
 - Jul 31, 2014
 
- Messages
 - 42
 
Hi MS Access Experts,
Please spare me a piece of your time to help me with this brain cracking code which will supposedly track the changes made in the table shown in a subform.
Note that the table in a subform is not locked and enabled so as to allow user to make changes although user has to access the form with a password. The only problem I have is that, it does not make any record of edits.
	
	
	
		
and here's the code under Before Update event:
	
	
	
		
 Please spare me a piece of your time to help me with this brain cracking code which will supposedly track the changes made in the table shown in a subform.
Note that the table in a subform is not locked and enabled so as to allow user to make changes although user has to access the form with a password. The only problem I have is that, it does not make any record of edits.
		Code:
	
	
	Sub AuditChanges(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 tbl_AuditTrail", cnn, adOpenDynamic, adLockOptimistic
    datTimeCheck = Now()
    strUserID = Forms!frmMainForm!txtUser
    Select Case UserAction
        Case "EDIT"
            For Each ctl In Screen.ActiveForm.ActiveControl.Form
                If ctl.Tag = "Audit" Then
                    If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
                        With rst
                            .AddNew
                            ![DateTime] = datTimeCheck
                            ![UserName] = strUserID
                            ![FormName] = Screen.ActiveForm.ActiveControl.Form.Name
                            ![Action] = UserAction
                            ![RecordID] = Screen.ActiveForm.ActiveControl.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] = Screen.ActiveForm.ActiveControl.Form.Name
                ![Action] = UserAction
                ![RecordID] = Screen.ActiveForm.ActiveControl.Form(IDField).Value
                .Update
            End With
    End Select
AuditChanges_Exit:
    On Error Resume Next
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub
AuditChanges_Err:
    MsgBox Err.Description, vbCritical, "ERROR!"
    Resume AuditChanges_Exit
End Sub
	
		Code:
	
	
	 Private Sub Form_BeforeUpdate(cancel As Integer)
     Call AuditChanges("ID")
 End Sub
	
			
				Last edited: