G
Guest
Hi there,
I am currently busy writing a bit of generic code for the company to use in
many of it's access databases. It is for maintaining an audit trail of al
changes to records.
I have the following code on the form that needs to have tracking:
Option Compare Database
Private Declare Function getUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, nSize As Long) As Long
Private Const MAX_BUFFER_LENGTH = 100
Private Function getLoggedUserName() As String 'User Defined Function
Dim strBufferString As String
Dim lngResult As Long
strBufferString = String(MAX_BUFFER_LENGTH, "X")
lngResult = getUserName(strBufferString, MAX_BUFFER_LENGTH)
getLoggedUserName = Mid(strBufferString, 1, MAX_BUFFER_LENGTH)
End Function
Private Sub Close_Form_Click()
On Error GoTo Err_Close_Form_Click
DoCmd.Close
Exit_Close_Form_Click:
Exit Sub
Err_Close_Form_Click:
MsgBox Err.Description
Resume Exit_Close_Form_Click
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error Resume Next
' some controls may not have the Tag property ,
' hence the resume next
Dim blnCheckDiff As Boolean
Dim ctl As Control
For Each ctl In Me.Controls
blnCheckDiff = False
If ctl.Tag = "Check" Then
If ctl.Value <> ctl.OldValue Then
blnCheckDiff = True
Else
blnCheckDiff = False
End If
Else
blnCheckDiff = False
End If
If blnCheckDiff Then
Dim db
Set db = CurrentDb
db.Execute ("Insert into [AuditTrail]
(Fieldname,OldValue,NewValue,RecordID,FormName,ChangedDate,UserName) values
('" & ctl.Name & "','" _
& ctl.OldValue & "' , '" & ctl.Value & "' , '" & Me![DrName] & "' ,
'" & Me.Name & "' , '" & Now() & "','" & getLoggedUserName & "') ")
Set db = Nothing
Else
Cancel = True
End If
Next
End Sub
The problem is that when the before_update is invoked, it executes, but the
form does not continue it's operation. For example, when changing a value and
navigating to the next record, the change is tracked, but the change is not
saved in the database, and the form does not go on to the next record.
If anyone can help with this I'd be most grateful.
I am currently busy writing a bit of generic code for the company to use in
many of it's access databases. It is for maintaining an audit trail of al
changes to records.
I have the following code on the form that needs to have tracking:
Option Compare Database
Private Declare Function getUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, nSize As Long) As Long
Private Const MAX_BUFFER_LENGTH = 100
Private Function getLoggedUserName() As String 'User Defined Function
Dim strBufferString As String
Dim lngResult As Long
strBufferString = String(MAX_BUFFER_LENGTH, "X")
lngResult = getUserName(strBufferString, MAX_BUFFER_LENGTH)
getLoggedUserName = Mid(strBufferString, 1, MAX_BUFFER_LENGTH)
End Function
Private Sub Close_Form_Click()
On Error GoTo Err_Close_Form_Click
DoCmd.Close
Exit_Close_Form_Click:
Exit Sub
Err_Close_Form_Click:
MsgBox Err.Description
Resume Exit_Close_Form_Click
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error Resume Next
' some controls may not have the Tag property ,
' hence the resume next
Dim blnCheckDiff As Boolean
Dim ctl As Control
For Each ctl In Me.Controls
blnCheckDiff = False
If ctl.Tag = "Check" Then
If ctl.Value <> ctl.OldValue Then
blnCheckDiff = True
Else
blnCheckDiff = False
End If
Else
blnCheckDiff = False
End If
If blnCheckDiff Then
Dim db
Set db = CurrentDb
db.Execute ("Insert into [AuditTrail]
(Fieldname,OldValue,NewValue,RecordID,FormName,ChangedDate,UserName) values
('" & ctl.Name & "','" _
& ctl.OldValue & "' , '" & ctl.Value & "' , '" & Me![DrName] & "' ,
'" & Me.Name & "' , '" & Now() & "','" & getLoggedUserName & "') ")
Set db = Nothing
Else
Cancel = True
End If
Next
End Sub
The problem is that when the before_update is invoked, it executes, but the
form does not continue it's operation. For example, when changing a value and
navigating to the next record, the change is tracked, but the change is not
saved in the database, and the form does not go on to the next record.
If anyone can help with this I'd be most grateful.