Results 1 to 11 of 11

Thread: Get system username ...

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Location
    Poland
    Posts
    10

    Get system username ...

    Hello

    Could you explain me:
    How can I get user name, I mean system user name NOT Application.Username.

    I need to detect user name. I can not use:
    Application.Username
    because this variable can be simply change by the user, for example:
    user: John1
    is able to change user name like this:
    Tools=>Options=>General_tab=>UserName

    Do you have any idea how I can get true user name?

    thanks a lot

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Get system username ...

    You can use the environmental variables...

    VB Code:
    1. Dim strUser
    2.   strUser = Environ("Username")
    3.   MsgBox strUser
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527

    Re: Get system username ...

    this is what I would use in VB. Should be the same for VBA?

    VB Code:
    1. 'API declaration at top of module
    2. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    3.  
    4. 'function for getting logged in system username
    5. Private Function GetCurrentNTUserName() As String
    6.  
    7. On Error GoTo Err_Handler
    8.  
    9.    Dim llReturn As Long
    10.    Dim lsUserName As String
    11.    Dim lsBuffer As String
    12.    
    13.    lsUserName = ""
    14.    lsBuffer = Space$(255)
    15.    llReturn = GetUserName(lsBuffer, 255)
    16.    If llReturn Then
    17.       lsUserName = Left$(lsBuffer, InStr(lsBuffer, vbNullChar) - 1)
    18.    End If
    19.    
    20.    GetCurrentNTUserName = lsUserName
    21.    
    22.    Exit Function
    23.  
    24. Err_Handler:
    25.    GetCurrentNTUserName = vbNullString
    26.    
    27. End Sub

  4. #4
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527

    Re: Get system username ...

    Quote Originally Posted by dannymking
    You can use the environmental variables...

    VB Code:
    1. Dim strUser
    2.   strUser = Environ("Username")
    3.   MsgBox strUser
    I don't believe it! All these years I've used the API to get it and it was that simple all along

  5. #5
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Get system username ...

    Blade,

    I used to use that too... But then the environment variables saved a lot of code
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  6. #6
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Get system username ...

    There's a whole list of them.. and this works in VBA, VB6.0 and VB.Net
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  7. #7
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527

    Re: Get system username ...

    I'm going to have to have a nosey round I think.
    Don't suppose there's one in there for computer name as well? (another one which I'm currently using the API for)

  8. #8
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527

    Re: Get system username ...

    tsk... it's all so simple (COMPUTERNAME) :s

    just found this bit of code for listing the environ variables

    VB Code:
    1. Private Sub Form_Load()
    2.      'Set form autoredraw property to true to get this example
    3.      'to work
    4.      
    5.      Dim myuser As Variant
    6.      Dim I As Integer
    7.      'used to test if any environment variables exist
    8.      'sometimes they don't
    9.      Dim sTemp As String
    10.      
    11.      I = 1
    12.      myuser = Environ(I)
    13.      sTemp = myuser
    14.      Form1.Print Environ(I)
    15.      I = I + 1
    16.      'Print all values on form
    17.      Do While Len(myuser) > 0
    18.           Form1.Print Environ(I)
    19.           I = I + 1
    20.           myuser = Environ(I)
    21.           sTemp = sTemp & myuser
    22.      Loop
    23.      If Len(sTemp) = 0 Then Form1.Print _
    24.          "No environment variables exist"
    25.      
    26. End Sub

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Location
    Poland
    Posts
    10

    Re: Get system username ...

    Thanks a lot - PERFECT answer
    Have a nice weekend

  10. #10
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Get system username ...

    I think there are a total of 34 of these.. At least on Vista there is
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get system username ...

    Be careful when relying upon the environment variables as these can be deleted and/or changed from what is accurate. I always recommend the APIs for getting either computer name and user name.

    Time taken to post - 30 seconds.
    Not having to work out issues from users changing environment variables - priceless.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width