Hello, I have no experience with VB or programming. I have copied some code from Candice Tripps link which is called Audit trail. I've added the modules as she shows in her example database and modified to suit. What I would like to do is on the form after a new record is inserted copy user name and changes to the Audit table. My form is designed for transactions purposes. The database is populated via the forms. I have 120 forms for this purpose.
Here is the code used:
Option Compare Database
Option Explicit
Private Sub Form_AfterInsert()
Dim x As Integer
If Not IsNull(Me!ID) Then
x = WriteAudit(Me, Me!ID)
End If
Dim stDocName As String
End Sub
Err_cmdClose_Click:
MsgBox Err.Description
Resume Exit_cmdClose_Click
End Sub
------------------------------------------------------------
Public Function WriteAudit(frm As Form, lngID As String) As Integer
On Error GoTo err_WriteAudit
Dim ctlC As Control
Dim strSQL As String
Dim bOK As Boolean
bOK = False
DoCmd.SetWarnings False
' For each control.
For Each ctlC In frm.Controls
If TypeOf ctlC Is TextBox Or TypeOf ctlC Is ComboBox Then
If ctlC.Value <> ctlC.OldValue Or IsNull(ctlC.OldValue) Then
If Not IsNull(ctlC.Value) Then
strSQL = "INSERT INTO AUDIT ( ID, FieldChanged, FieldChangedFrom, FieldChangedTo, User, DateofHit ) " & _
" SELECT " & lngID & " , " & _
"'" & ctlC.Name & "', " & _
"'" & ctlC.OldValue & "', " & _
"'" & ctlC.Value & "', " & _
"'" & GetUserName_TSB & "', " & _
"'" & Now & "'"
'Debug.Print strSQL
DoCmd.RunSQL strSQL
End If
End If
End If
Next ctlC
WriteAudit = bOK
exit_WriteAudit:
DoCmd.SetWarnings True
Exit Function
err_WriteAudit:
MsgBox Err.Description
Resume exit_WriteAudit
End Function
-----------------------------------------------------------
When I select any control I receive "The expression On Click you entered as the event property setting produced the following error: Member already exists in an object module from which this object module derives."
*The expression may not result in the name of a macro, the name of a user-defined functon, or [Event Procedure]
*There may have been an error evaluating the function, event, or macro.
Any help on this problem would be greatly appreciated.
Here is the code used:
Option Compare Database
Option Explicit
Private Sub Form_AfterInsert()
Dim x As Integer
If Not IsNull(Me!ID) Then
x = WriteAudit(Me, Me!ID)
End If
Dim stDocName As String
End Sub
Err_cmdClose_Click:
MsgBox Err.Description
Resume Exit_cmdClose_Click
End Sub
------------------------------------------------------------
Public Function WriteAudit(frm As Form, lngID As String) As Integer
On Error GoTo err_WriteAudit
Dim ctlC As Control
Dim strSQL As String
Dim bOK As Boolean
bOK = False
DoCmd.SetWarnings False
' For each control.
For Each ctlC In frm.Controls
If TypeOf ctlC Is TextBox Or TypeOf ctlC Is ComboBox Then
If ctlC.Value <> ctlC.OldValue Or IsNull(ctlC.OldValue) Then
If Not IsNull(ctlC.Value) Then
strSQL = "INSERT INTO AUDIT ( ID, FieldChanged, FieldChangedFrom, FieldChangedTo, User, DateofHit ) " & _
" SELECT " & lngID & " , " & _
"'" & ctlC.Name & "', " & _
"'" & ctlC.OldValue & "', " & _
"'" & ctlC.Value & "', " & _
"'" & GetUserName_TSB & "', " & _
"'" & Now & "'"
'Debug.Print strSQL
DoCmd.RunSQL strSQL
End If
End If
End If
Next ctlC
WriteAudit = bOK
exit_WriteAudit:
DoCmd.SetWarnings True
Exit Function
err_WriteAudit:
MsgBox Err.Description
Resume exit_WriteAudit
End Function
-----------------------------------------------------------
When I select any control I receive "The expression On Click you entered as the event property setting produced the following error: Member already exists in an object module from which this object module derives."
*The expression may not result in the name of a macro, the name of a user-defined functon, or [Event Procedure]
*There may have been an error evaluating the function, event, or macro.
Any help on this problem would be greatly appreciated.