'Add the microsoft windows command controls and drop a statusbar to the form
'Paste the following code into the code section of the form.
Option Explicit
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Form_Load()
Dim sUser As String
Dim sComputer As String
Dim lpBuff As String * 1024
'Get the Login User Name
GetUserName lpBuff, Len(lpBuff)
sUser = Left$(lpBuff, (InStr(1, lpBuff, vbNullChar)) - 1)
lpBuff = ""
'Get the Computer Name
GetComputerName lpBuff, Len(lpBuff)
sComputer = Left$(lpBuff, (InStr(1, lpBuff, vbNullChar)) - 1)
lpBuff = ""
StatusBar1.Panels(2).Text = sComputer & " : " & sUser
End Sub
---------------------------------------------------
Some of this code was copied from faq222-429 Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
Hi James,
Are you thinking of displaying the user name of the user who is logged into the application you have created or the user name of the user who is currently logged into Windows?
If you are trying to display the user name of the user logged into your application, then, have a text box in the bottom corner of your form. When the user name/password combination is successfully authenticated, display the username the user typed in the text box. You can lock the box so that the user does not change it midway before logging out. When the user logs out, assign an empty string to the text box.
If it is the user name of the user logged into Windows, then there is an API call that you must use to get that info. The other things can remain the same as in the previous case.
Let me know what happens or post again if you want more help.
If it's the NT or similar username you're after then using "Environ" is your way forward.
I've developed applications that first need to identify who is logged on to the machine, and I use the following code usually.....
public strUser as String
strUser = Right(Environ("USERNAME", 7)
Users here log in with a username that relates to a 7 figure identity number, hence the "7" in the line of code. I then relate the 7 figure number to a personnel database to get the individuals name.
I get a debug suggestion with this line
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
[/colour]
Any idea's, Im using Visual Basic 6 learning edition, does this have anything to do with it??? James Mannion
IT Officer
mannion@whitecross.hereford.sch.uk
Hmmm I'm not sure if the cause could be the learning edition. What is the error message?
You can replace the API call with the environ that HaworthBantam suggested:
---------------------------------------------------
'Add the microsoft windows command controls and drop a statusbar to the form
'Paste the following code into the code section of the form.
Option Explicit
Private Sub Form_Load()
Dim sComputer As String
Dim lpBuff As String * 1024
StatusBar1.Panels(2).Text = Environ("USERNAME"
End Sub
---------------------------------------------------
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
Your Last suggestion has got me closer but it only display the date! Not sure why maybe you do!! James Mannion
IT Officer
mannion@whitecross.hereford.sch.uk
It might be your learning edition that causes problems, try this to test the environ function:
Make a new project.
Put a button on the form.
Paste:
-------------------
Private Sub Command1_Click()
MsgBox Environ("USERNAME"
End Sub
-------------------
Run the project and press teh button. You should get a message box with you username.
2nd thought: which windows version do you use?
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
hmm confused because this works, click on the button and it pops back in another tiny form 'mannion' which is my logon name and proves it works!!! James Mannion
IT Officer
mannion@whitecross.hereford.sch.uk
StatusBar1.Panels(2).Text = Environ("USERNAME"
End Sub
----------------------------------------- Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
Your welcome -and if you are that happy, click the 'Mark this post as helpful' - and you'll make me happy too... Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.