kfschaefer
Registered User.
- Local time
- Yesterday, 16:40
- Joined
- Oct 10, 2008
- Messages
- 58
I need to keep an audit trail of certain fields on different forms that will on before update of a field capture the original value - compare to new value if different create an audit trail of both the original value and the new values for different records on the Audit Trail log. I need to capture the changes of the Work Statement No(WSNo) and Measurement No (MN).
If the MN is being assigned to a current WSNo then it will be considered an "Add".
If the MN is being assigned to a new WSNo then it will be considered an "Add".
If the WSNo is replacing a current MN with a new MN then it will be considered an "Add", however the previous value should be captured as a "Delete"
If the WSNo has MN's assigned and the WSNo is deleted then I need to create a list of those MN's and set them as Deletes in the Audit Trail Log, along with noting that the WSNO is a "Delete".
I hope I made this not to difficult to understand. I have looked at the "How to create an Audit trail at MS website - not sure how to modify it to be applicable to my needs since I only want to capture certain fields.
I found the example at Techrepublic website and I am attempting to modify for my needs and still need some assistance, please.
http://articles.techrepublic.com.com/5100-...11-6166807.html
after modifying the following code - I have run into an issue with handling the before value when it is NULL or empty. I attempted to use the NZ function but it still fails.
If the MN is being assigned to a current WSNo then it will be considered an "Add".
If the MN is being assigned to a new WSNo then it will be considered an "Add".
If the WSNo is replacing a current MN with a new MN then it will be considered an "Add", however the previous value should be captured as a "Delete"
If the WSNo has MN's assigned and the WSNo is deleted then I need to create a list of those MN's and set them as Deletes in the Audit Trail Log, along with noting that the WSNO is a "Delete".
I hope I made this not to difficult to understand. I have looked at the "How to create an Audit trail at MS website - not sure how to modify it to be applicable to my needs since I only want to capture certain fields.
I found the example at Techrepublic website and I am attempting to modify for my needs and still need some assistance, please.
http://articles.techrepublic.com.com/5100-...11-6166807.html
after modifying the following code - I have run into an issue with handling the before value when it is NULL or empty. I attempted to use the NZ function but it still fails.
Code:
Option Compare Database
Option Explicit
Const cDQ As String = """"
Sub AuditTrail(frm As Form)
'Track changes to data.
'recordid identifies the pk field's corresponding
'control in frm, in order to id record.
Dim ctl As Control
Dim varBefore As Variant
Dim varAfter As Variant
Dim strControlName As String
Dim strSQL As String
On Error GoTo ErrHandler
For Each ctl In frm.Controls
If ctl.Tag = "Audit" Then
varBefore = Nz(ctl.oldvalue)
Debug.Print varBefore
varAfter = Nz(ctl.Value)
Debug.Print varAfter
If varAfter <> varBefore Then
strControlName = Nz(ctl.Name)
Debug.Print ctl.Name
'Build INSERT INTO statement.
strSQL = "INSERT INTO " _
& "Audit (EditDate, User, ApNo, WSNO, MeasNO, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (Now()," _
& cDQ & Environ("username") & cDQ & ", " _
& cDQ & GetgApNo() & cDQ & ", " _
& cDQ & GetgWSNO() & cDQ & ", " _
& cDQ & GetgMeasNo() & cDQ & ", " _
& cDQ & strControlName & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
DoCmd.RunSQL strSQL
Debug.Print strSQL
End If
End If
Next ctl
Set ctl = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description & vbNewLine _
& Err.Number, vbOKOnly, "Error"
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Form_BeforeUpdate_Error
gApNo = Me.ApNo
gWSNO = Me.WS_No
Call AuditTrail(Me)
On Error GoTo 0
Exit Sub
Form_BeforeUpdate_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_BeforeUpdate of VBA Document Form_FE_WS_Main"
End Sub