Logging Database Users

  • Thread starter Thread starter ceejay14
  • Start date Start date
C

ceejay14

Guest
Hey everyone,
I’m a newbie to access security and it’s my first time posting, but I’ve been surfing around here for a while and I’ve learned a lot by visiting here. The database that I am currently working on has Access user-level security implemented with 3 user groups with permissions assigned: Admin, Read-only and Full Data Users.
My question is in regard to logging the database users in a table. I have already performed a search on this topic and was able to obtain success with this code

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String, db As Database, r As Recordset
'rst As Recordset
'Dim table2 As String
Set db = CurrentDb
Set r = db.OpenRecordset("UserLog") 'Open the table User Log

strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If lngX <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = ""
End If

r.AddNew
r.Fields("UserID") = UCase(fOSUserName)
r.Fields("EntryTime") = Now()
r.Update

My question is that opposed to logging the network log-in name of the user, is there a way to log the actual username of the user? For instance if I have a user called ‘ap01’ in my workgroup file can I have this name show in my UserLog table?

Whatever help you can provide would be great. Thank you
 
The code youre using above will only give the network name which, from what I can gather, youre not really interested in.

The method I use (rightly or wrongly) is to open a form called frmlogusers which is based on a table called tbllogusers and stamp the user and date/time on it like this:

Private Sub Form_Open(Cancel As Integer)

UserName = CURRENTUSER() 'Set the Username textbox on the opening form to the currentuser
Form.Refresh
DoCmd.OpenForm "frmLogUsers", acNormal, , , , acWindowNormal 'Open the logusers form
DoCmd.GoToRecord , , acNewRec 'Go to a new record
Forms!frmLogUsers!UserName = CURRENTUSER() 'Set the username on the logusers form to the currentuser
Forms!frmLogUsers!LogonDate = Now() 'Set the logon date to the current date and time
DoCmd.CLOSE

End Sub


Then when the user logs off, I open this form again and go to the first record that matches the currentuser (this is got by sorting by logon date in a query):

Private Sub Exit_Database_Click()
On Error GoTo Err_Exit_Database_Click

DoCmd.OpenForm "frmLogUsers", acNormal 'Open form
Forms!frmLogUsers!UserName.SetFocus 'Set focus on the username field
DoCmd.FindRecord fOSUserName 'Go to the first record with the corresponding current username

Forms!frmLogUsers!LogoffDate = Now() 'Set the logoff date to the current date and time

DoCmd.CLOSE
DoCmd.Quit

Exit_Exit_Database_Click:
Exit Sub

Err_Exit_Database_Click:
MsgBox Err.Description
Resume Exit_Exit_Database_Click
End Sub
 
thanks

Hi guys.. MadMaxx I'm from Ontario and Andrew thanks for the response. I was able to fix the problem with the help of the CurrentUser method. Thanks for pointing that out. :)
 

Users who are viewing this thread

Back
Top Bottom