Windows Domain server/Novell NetWare logon (may be different from Windows logon)
Using environment variables to retrieve the username
A point about case sensitivity
1. Access/JET security logon
Use the CurrentUser() function to return the currently-logged on user to your database workgroup. If the database is not using Access/JET security, the default user is "Admin", and thus CurrentUser() will return "Admin".
example: txtCreatedBy.Value = CurrentUser()
2. Local Windows logon
This returns the Windows username for the local machine. This is not necessarily the same value as the network logon ID, though most of the time it is.
To get this value, you must make an API call to grab the value. One implementation that neatly takes care of the API declarations and coding is available at the Access MVP web site:
'source: http://groups.google.com/groups?selm=387490BE.1CD2C4D3%40barneyboller.com Public Declare Function WNetGetUser Lib "mpr.dll" _ Alias "WNetGetUserA" (ByVal lpName As String, _ ByVal lpUserName As String, lpnLength As Long) As Long
Const NoError = 0 'The Function call was successful
Public Function apiNetUserName() As String
' Buffer size for the return string. Const lpnLength As Integer = 255 ' Get return buffer space. Dim status As Integer ' For getting user information. Dim lpName, lpUserName As String ' Assign the buffer size constant to lpUserName. lpUserName = Space$(lpnLength + 1) ' Get the log-on name of the person using product. status = WNetGetUser(lpName, lpUserName, lpnLength) ' See whether error occurred. If status = NoError Then ' This line removes the null character. Strings in C are null- ' terminated. Strings in Visual Basic are not null-terminated. ' The null character must be removed from the C strings to be used ' cleanly in Visual Basic. lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1) Else ' An error occurred. lpUserName = "" End End If FindNetUserName = lpUserName End Function
txtCreatedBy.Value = apiNetUserName()
4. Computer name This is also handled via an API call, and the excellent Access MVPs web site has this available as well:
5. Using environment variables By far the simplest method to return the username, you may simply use the function Environ("USERNAME") to retrieve the Windows username.
example: txtCreatedBy.Value = Environ("USERNAME")
This can be problematic, however. Some operating systems do not automatically set this environment variable (Windows 95, 98, and ME). Additionally, users can change environment variables with a simple DOS command. To try this yourself, type
and now Environ("USERNAME") will return "BILLG" until you set the environment variable to something else or reboot the machine.
Note that depending on how you type in your username when logging in, you may get your username returned as "billg" or "Billg" or "BILLG". If you wish, wrap your function calls with UCase() or LCase() to ensure that you get your username formatted as you like.
This way, if you are comparing the current user to a table of users, you can directly check