Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: Access Other topics FAQ

VBA and Custom Functions

Getting all types of Usernames in Access by psemianonymous
Posted: 25 Jun 03 (Edited 5 Aug 03)

  1. Access/JET security logon
  2. Local Windows logon
  3. Windows Domain server/Novell NetWare logon (may be different from Windows logon)
  4. Computer name
  5. Using environment variables to retrieve the username
  6. 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".

    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:


    txtCreatedBy.Value = fOSUserName()

NOTE: Please do not be confused by their confusing titles and page headers.  This returns the local Windows username, not the network username.  See the following newsgroup thread for an example of when the local username and network username are different: http://groups.google.com/groups?th=6e618210e62275b&seekm=Xns93CDD96176A8Edfurmancloud99%40199.184.165.241#Link1

3.  NT/Netware logon
This returns the network username via an API call.  You may paste the following code into a new module and use the apiNetUserName() function.

'-----------begin copying below-----------------
Option Explicit

'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)
           ' An error occurred.
           lpUserName = ""
        End If
        FindNetUserName = lpUserName
End Function

'-----------------stop here---------------

    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:


    txtComputerName.Value = fOSMachineName()

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.  

    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.

6.  Case-sensitivity

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

strSQL = strSQL & [USERID] = " & Chr(34) & UCase(CurrentUser()) & Chr(34)

instead of using the "LIKE" operator.

Back to Microsoft: Access Other topics FAQ Index
Back to Microsoft: Access Other topics Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close