View Full Version : Use User ID for Auditing


fenhow
07-24-2007, 05:57 PM
Hello,
Can someone please tell me how or assist me on how to use the Username I capture when someone logs into the database for the following audit code? It continues to call everyone "Admin" I am using the following code to manage my users:

LOG ON FORM

Private Sub LogOn_AfterUpdate()
On Error GoTo Err_LogOn

User.UserID = DLookup("UserID", "tblUser", "UserName = '" & [LogOn] & "'")
User.UserPassword = DLookup("UserPassword", "tblUser", "UserName = '" & [LogOn] & "'")
User.UserLevel = DLookup("UserLevelID", "tblUser", "UserName = '" & [LogOn] & "'")
User.UserName = Me.LogOn

Me.ConfirmPassword = Null
Me.ConfirmPassword.SetFocus

Exit_LogOn_AfterUpdate:
Exit Sub

Err_LogOn:
If Err.Number = 94 Then 'Invalid use of Null - Means no user name found
Resume Exit_LogOn_AfterUpdate

Else

MsgBox Err.Description
Resume Exit_LogOn_AfterUpdate

End If

End Sub

---------------------------

AUDIT CODE MODULE

Public Function Audit_Trail()
On Error GoTo Err_Audit_Trail

'ACC2000: How to Create an Audit Trail of Record Changes in a Form
'http://support.microsoft.com/default.aspx?scid=kb;en-us;197592
Function AuditTrail(frm As Form)

'Dim MyForm As Form
Dim ctl As Control
Dim UserName As String


'Set MyForm = Screen.ActiveForm
'UserID = "User: " & UserID 'You need to identify your users if you are not using Access security with workgroups.
UserName = CurrentUser

'If new record, record it in audit trail and exit function.
If frm.NewRecord = True Then
frm!AuditTrail = frm!tbAuditTrail & "New Record added on " & Now & " by " & UserName & ";"
Exit Function
End If

'Set date and current user if the form (current record) has been modified.
frm!AuditTrail = frm!tbAuditTrail & vbCrLf & vbLf & "Changes made on " & Now & " by " & UserName & ";"

'Check each data entry control for change and record old value of the control.
For Each ctl In frm.Controls

'Only check data entry type controls.
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox
If ctl.Name = "tbAuditTrail" Then GoTo TryNextControl 'Skip AuditTrail field.
'If new and old value do not equal
If ctl.Value <> ctl.OldValue Then
frm!AuditTrail = frm!tbAuditTrail & vbCrLf & ctl.Name & ": Changed From: " & ctl.OldValue & ", To: " & ctl.Value
'If old value is Null and new value is not Null
ElseIf IsNull(ctl.OldValue) And Len(ctl.Value) > 0 Or ctl.OldValue = "" And Len(ctl.Value) > 0 Then
frm!AuditTrail = frm!tbAuditTrail & vbCrLf & ctl.Name & ": Was Previoulsy Null, New Value: " & ctl.Value
'If new value is Null and old value is not Null
ElseIf IsNull(ctl.Value) And Len(ctl.OldValue) > 0 Or ctl.Value = "" And Len(ctl.OldValue) > 0 Then
frm!AuditTrail = frm!tbAuditTrail & vbCrLf & ctl.Name & ": Changed From: " & ctl.OldValue & ", To: Null"
End If
End Select

Thank you

Fen How

fenhow
07-24-2007, 06:05 PM
Here it is for those of you looking at this thread and need the help.

UserName = User.UserName

Fen How