Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display a Username 1

Status
Not open for further replies.

mannion

IS-IT--Management
Jun 24, 2002
73
GB
Hi,

I need some help, I need to be able to display a username in the bottom right hand corner of a form I have made. I would I go about this????

Thanks James Mannion
IT Officer
mannion@whitecross.hereford.sch.uk
 

'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

StatusBar1.Panels.Add
StatusBar1.Panels(1).AutoSize = sbrSpring
StatusBar1.Panels(2).Alignment = sbrRight
StatusBar1.Panels(1).Style = sbrDate

'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.

Regards,
tlv
 
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.
 
Thanks people,

Firstly I'm trying to display the name of the person logged into windows on my application. So what would I need to do now???

Thanks people James Mannion
IT Officer
mannion@whitecross.hereford.sch.uk
 
Try my suggestion above... Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
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.Add
StatusBar1.Panels(1).AutoSize = sbrSpring
StatusBar1.Panels(2).Alignment = sbrRight
StatusBar1.Panels(1).Style = sbrDate

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
 
Try:
----------------------------------------
Private Sub Form_Load()
StatusBar1.Panels.Add
StatusBar1.Panels(1).AutoSize = sbrSpring
StatusBar1.Panels(1).MinWidth = 1
StatusBar1.Panels(2).Alignment = sbrRight
StatusBar1.Panels(1).Style = sbrDate

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'
 
All I can say is you are a star, works like a treat!!!!!!
If I new ya the beers would be on me!!!

Cheers for all your help Sunaj!!!! James Mannion
IT Officer
mannion@whitecross.hereford.sch.uk
 
Your welcome -and if you are that happy, click the 'Mark this post as helpful' - and you'll make me happy too...
[pipe] Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top