View Full Version : Display Username in unbound text box on a form
XopieX20
03-20-2014, 07:06 PM
OK, I have a database that works great so far. I have about 80 users all with thier own Usernames and Passwords. Issue is, I want to be able to catch the Username they log in with, and then display it in a textbox on the form they go to after login. Not to good with VBA, but I know it can be done. Please let me know if anyone has any ideas.... Keep in mind, I'm not to good with VBA yet so please explain :) THANKS.
In case you need it, here is my login code.
Private Sub Command8_Click()
Username.SetFocus
'This will check usernames and passwords in the system
If Username = "Admin" And Password = "123" Then
DoCmd.Close
DoCmd.OpenForm "WrittenStatsForm"
Else
MsgBox "Incorrect Username and or Password"
End If
End Sub
I'll just assume you're loading usernames and passwords from a table since you didn't specify.
Once the user has logged in you can store their username in a public variable in a module.
Then you just need to create a public function to read that variable.
Public LoggedInUser As String
Public Function GetUserName() As String
GetUserName = LoggedInUser
'or to get windows login use
'GetUserName = CreateObject("WScript.Network").UserName
End Function
You can use public functions pretty much anywhere, macros, queries, etc.
For a textbox just set the control source to =GetUserName()
I'll also assume you're asking for help with the data loading...
This example requires two text boxes, txtUser and txtPW.
When the form opens, keypreview is set to true, which means that whenever the user presses a button the form gets to process the event before the control. I've used it here to trap the Return key which has a keycode of 13.
Whenever the user presses the return key, CheckPW checks that neither text box is empty then opens a recordset to get the password from a table called 'user'.
It then does a case sensitive check against both passwords. If they match, CheckPW is set to true, the user is logged in and the public username variable is set.
Private Sub Form_Load()
Me.KeyPreview = True
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 13
If CheckPW(Trim("" & txtUser), Trim("" & txtPW)) Then
LoggedInUser = Trim("" & txtUser)
DoCmd.OpenForm "WrittenStatsForm"
DoCmd.Close acForm, Me.Name
End If
End Select
End Sub
Private Function CheckPW(usr As String, pw As String) As Boolean
If usr = "" Then Exit Function
If pw = "" Then Exit Function
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim retval As String
Set db = CurrentDb
Set rs = db.OpenRecordset("select pw from user where username='" & usr & "'")
If Not rs.EOF Then retval = rs("pw").Value
rs.Close
Set rs = Nothing
Set db = Nothing
CheckPW = StrComp(retval, pw, vbBinaryCompare) = 0
End Function
Bob Phillips
03-21-2014, 07:11 AM
Use
Environ("Username")
Use
Environ("Username")
Yeah that should also return the windows logon username.
But I've had problems with that in the past so I prefer to use the WScript.Network UserName instead (commented out option in first piece of code).
Bob Phillips
03-21-2014, 08:21 AM
Really? I use it all of the time. What problems have you had? I've had problems with WScript on sites where scripting is disabled (not often, but it has happened).
XopieX20
03-21-2014, 08:53 AM
Use
Environ("Username") OK, I'm new with VBA so can you explain a little? I have 1 form that is a login form, once they sign in, that form closes and opens another form they will use. Where do I put your code to get the username and how do I get it in the text box on the main form they are using?
Create a public function as I said in my first post but just use
Public Function GetUserName() As String
GetUserName = environ("UserName")
End Function
@xld
I just used to find that it would sometimes return no value. You're right though it's the best option if it works.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.