|
-
Aug 6th, 2005, 06:21 AM
#1
Thread Starter
New Member
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
-
Aug 6th, 2005, 06:31 AM
#2
Re: Get system username ...
You can use the environmental variables...
VB Code:
Dim strUser
strUser = Environ("Username")
MsgBox strUser
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Aug 6th, 2005, 06:31 AM
#3
Fanatic Member
Re: Get system username ...
this is what I would use in VB. Should be the same for VBA?
VB Code:
'API declaration at top of module
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
'function for getting logged in system username
Private Function GetCurrentNTUserName() As String
On Error GoTo Err_Handler
Dim llReturn As Long
Dim lsUserName As String
Dim lsBuffer As String
lsUserName = ""
lsBuffer = Space$(255)
llReturn = GetUserName(lsBuffer, 255)
If llReturn Then
lsUserName = Left$(lsBuffer, InStr(lsBuffer, vbNullChar) - 1)
End If
GetCurrentNTUserName = lsUserName
Exit Function
Err_Handler:
GetCurrentNTUserName = vbNullString
End Sub
-
Aug 6th, 2005, 06:33 AM
#4
Fanatic Member
Re: Get system username ...
 Originally Posted by dannymking
You can use the environmental variables...
VB Code:
Dim strUser
strUser = Environ("Username")
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
-
Aug 6th, 2005, 06:34 AM
#5
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
-
Aug 6th, 2005, 06:35 AM
#6
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
-
Aug 6th, 2005, 06:36 AM
#7
Fanatic Member
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)
-
Aug 6th, 2005, 06:39 AM
#8
Fanatic Member
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:
Private Sub Form_Load()
'Set form autoredraw property to true to get this example
'to work
Dim myuser As Variant
Dim I As Integer
'used to test if any environment variables exist
'sometimes they don't
Dim sTemp As String
I = 1
myuser = Environ(I)
sTemp = myuser
Form1.Print Environ(I)
I = I + 1
'Print all values on form
Do While Len(myuser) > 0
Form1.Print Environ(I)
I = I + 1
myuser = Environ(I)
sTemp = sTemp & myuser
Loop
If Len(sTemp) = 0 Then Form1.Print _
"No environment variables exist"
End Sub
-
Aug 6th, 2005, 06:44 AM
#9
Thread Starter
New Member
Re: Get system username ...
Thanks a lot - PERFECT answer
Have a nice weekend
-
Aug 6th, 2005, 06:51 AM
#10
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
-
Aug 6th, 2005, 12:43 PM
#11
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|