We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


debug GetUserName - Getting username and description

new_wes asked
Medium Priority
Last Modified: 2012-05-05
hi - adopted from

CLASS Module:
Option Compare Database
Declare Function GetUserName Lib "advapi32.dll" Alias _
       "GetUserNameA" (ByVal lpBuffer As String, _
       ByRef nSize As Integer) As Integer

FORM Module:
' Return the user's name.
Private Function GUserName() As String
Const UNLEN = 256   ' Max user name length.
Dim user_name As String
Dim name_len As Integer
    name_len = Len(user_name)
    user_name = Space$(UNLEN + 1)
    If GetUserName(user_name, name_len) = 0 Then
        GUserName = "Unknown"
        MsgBox ("Msg1 user_name=" & user_name & ". GUserName=" & GUserName & ".")
        GUserName = Left$(user_name, name_len - 1)
        MsgBox ("Msg2 user_name=" & user_name & ". GUserName=" & GUserName & ".")
    End If
End Function

Private Sub Form_Open(Cancel As Integer)
    Me.txtGUser = GUserName
End Sub

First messagebox and Always returns field txtGUser = "Unknown"

If i change conditional "If GetUserName..." to "> 0", then returns nothing (no msgbox etc)

1. if this works, shouldn't this ultimately return "Jane Smith" instead of ntuser "jsm057"?

2. i'm assuming that GetUserName function returns greater than zero if finds user?  either way, i can't see how this gets the user name/description into the GUserName string?

3. next step to debug??
Watch Question

Try switching the order of these lines:

  name_len = Len(user_name)
  user_name = Space$(UNLEN + 1)


hmm - think i see what you mean but no effect - GetUserName always seems to return 0 . . .
Most Valuable Expert 2014
This is the one I use. It returns the WinNT user name.
Option Compare Database
Option Explicit

'From http://support.microsoft.com/default.aspx?scid=kb;en-us;161394
 ' Declare for call to mpr.dll.
   Declare Function WNetGetUser Lib "mpr.dll" _
      Alias "WNetGetUserA" (ByVal lpName As String, _
      ByVal lpUsersName As String, lpnLength As Long) As Long

   Const NoError = 0       'The Function call was successful

Public Function GetUsersName() 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, lpUsersName As String

      ' Assign the buffer size constant to lpUsersName.
      lpUsersName = Space$(lpnLength + 1)

      ' Get the log-on name of the person using product.
      Status = WNetGetUser(lpName, lpUsersName, 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.
         lpUsersName = Left$(lpUsersName, InStr(lpUsersName, Chr(0)) - 1)

         ' An error occurred.
         MsgBox "Unable to get the name."
      End If

      ' Display the name of the person logged on to the machine.
'      MsgBox "The person logged on this machine is: " & lpUsersName
    GetUsersName = lpUsersName
End Function

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Examining closer, isn't this a little tighter:

Private Function GUserName() As String
Dim user_name As String * 257
    If GetUserName(user_name, len(user_name)) > 0 Then
         GUserName = Left$(user_name, InStr(1, user_name, vbNullChar) - 1)
        GUserName = "Unknown"
    End If  
    MsgBox ("Msg2 user_name=" & user_name & ". GUserName=" & GUserName & ".")
End Function



hi jimpen & dgmg - thanks, got both working, but both look like return same as Environ("UserName") ??  

if that's as far as we can get that's okay, just thought there would be a way to pull more from user profile??  active driectory and all etc.
Most Valuable Expert 2014

You would have to develop LDAP functiions to get to that.


well that counts me out!    

thanks all - tried to split points best i could -


I tested your exact original code and it gave me compile errors because of the function declaration.  Here is what I use:

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

Most Valuable Expert 2014

Note that the Environ("UserName") doesn't always populate correctly.

The way we pull it off (w/o LDAP) is that we have one centralized employee DB in access. Then we link the employees table to whatever DB we're working in.  And pull data from that.

Sorry I couldn't give a better answer. May all your days get brighter and brighter.


i'll second that for all!  

Also, Environ("UserName") is not secure.  One can easily modify the environment without a password and masquerade as someone else.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.