469,591 Members | 1,989 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,591 developers. It's quick & easy.

how to retrieve Current user from a table..

i have audit trail module using this code...

Option Compare Database
Option Explicit

Public Function AuditTrail()
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

Dim MyForm As Form
Dim ctl As Control
Dim sUser As String
Set MyForm = Screen.ActiveForm
' sUser = "User: " & UsersID 'You need to identify your users if you are not using Access security with workgroups.
sUser = CurrentUser()
'If new record, record it in audit trail and exit function.
If MyForm.NewRecord = True Then
MyForm!Updates = MyForm!tbAuditTrail & "New Record added on " & Now & " by " & sUser & ";"
Exit Function
End If

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

'Check each data entry control for change and record old value of the control.
For Each ctl In MyForm.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
MyForm!Updates = MyForm!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
MyForm!Updates = MyForm!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
MyForm!Updates = MyForm!tbAuditTrail & vbCrLf & ctl.Name & ": Changed From: " & ctl.OldValue & ", To: Null"
End If
End Select

TryNextControl:
Next ctl

Exit_Audit_Trail:
Exit Function

Err_Audit_Trail:
If Err.Number = 64535 Then 'Operation is not supported for this type of object.
Exit Function
ElseIf Err.Number = 2475 Then 'You entered an expression that requires a form to be the active window
Beep
MsgBox "A form is required to be the active window!", vbCritical, "Invalid Active Window"
Else
Beep
MsgBox Err.Number & " - " & Err.Description
End If
Resume Exit_Audit_Trail

End Function

I HAVE A LOGON FORM THAT RETRIVES DATA FROM A TABLE.
IS IT POSSIBLE TO RETRIEVE THE CURRENT USER FROM A TABLE TO BE USED FOR MY AUDIT TRAIL.
Apr 23 '07 #1
7 3108
ADezii
8,800 Expert 8TB
i have audit trail module using this code...

Option Compare Database
Option Explicit

Public Function AuditTrail()
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

Dim MyForm As Form
Dim ctl As Control
Dim sUser As String
Set MyForm = Screen.ActiveForm
' sUser = "User: " & UsersID 'You need to identify your users if you are not using Access security with workgroups.
sUser = CurrentUser()
'If new record, record it in audit trail and exit function.
If MyForm.NewRecord = True Then
MyForm!Updates = MyForm!tbAuditTrail & "New Record added on " & Now & " by " & sUser & ";"
Exit Function
End If

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

'Check each data entry control for change and record old value of the control.
For Each ctl In MyForm.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
MyForm!Updates = MyForm!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
MyForm!Updates = MyForm!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
MyForm!Updates = MyForm!tbAuditTrail & vbCrLf & ctl.Name & ": Changed From: " & ctl.OldValue & ", To: Null"
End If
End Select

TryNextControl:
Next ctl

Exit_Audit_Trail:
Exit Function

Err_Audit_Trail:
If Err.Number = 64535 Then 'Operation is not supported for this type of object.
Exit Function
ElseIf Err.Number = 2475 Then 'You entered an expression that requires a form to be the active window
Beep
MsgBox "A form is required to be the active window!", vbCritical, "Invalid Active Window"
Else
Beep
MsgBox Err.Number & " - " & Err.Description
End If
Resume Exit_Audit_Trail

End Function

I HAVE A LOGON FORM THAT RETRIVES DATA FROM A TABLE.
IS IT POSSIBLE TO RETRIEVE THE CURRENT USER FROM A TABLE TO BE USED FOR MY AUDIT TRAIL.
__1. Declare a Public Variable to represent the Current User in a Standard Code Module as in:
Expand|Select|Wrap|Line Numbers
  1. Public strCurrentUser As String
__2. Capture the User's Name from the LOGON Form and assign it to the Puiblic Variable as in:
Expand|Select|Wrap|Line Numbers
  1. strCurrentUser = "<name extracted from logon form>"
__3. Now you can refer to the Current User anywhere in your application by referencing the Public Variable assigned to him/her, namely, strCurrentUser.
Apr 23 '07 #2
how will the function determine the active current user?
Apr 23 '07 #3
how will i exactly extract the logged user...ty
Apr 23 '07 #4
ADezii
8,800 Expert 8TB
how will the function determine the active current user?
__1. The Current Active User of the Database will always be returned by the CurrentUser() Function, the Current User as defined by the Operating System will be returned by Environ("UserName").
Apr 23 '07 #5
the curentuser() give me the default ms access security workgroup users..i want the values to be retrive from my table which i have used as reference of my log in form.
Apr 24 '07 #6
were geting near...hehe...environ("the selected user text value logged from combo box on a log in form") how will i exactly do this man..i really appreciate your help
Apr 24 '07 #7
Help Please Im Stocked
Apr 26 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by Daniel Tonks | last post: by
5 posts views Thread by Jim Richards | last post: by
3 posts views Thread by Christopher Weaver | last post: by
2 posts views Thread by rn5a | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.