Populate Unbound Textbox with User Name

KLahvic01

Registered User.
Local time
Yesterday, 21:51
Joined
May 3, 2012
Messages
80
I have an unbound textbox on a form and I would like it to show the logged in user to the databases first and last name. I have a table called tblUser where all users login information is stored, and a log in screen that runs a security level check against the table. What I would like is to show whoever is logged in the unbound textbox. Almost like a welcome back "user". Can someone help, all the posts I have found seem to be geared around windows login and I am not interested in that.

Thanks,

KJ
 
What are the field names in tblUser?
 
Thanks for the quick reply. The field Names in tblUser are:

UserId (primary key) - Auto Number
FName - Text
LName - Text
Password - Text
PWReset - Yes/No
AccessLevel - Number
Role - Text

Hope this helps, let me know if you need other information.

KJ
 
Ok what I would do is either when the user logs in using your login screen you could store his login name in a tempvar and then just call the tempvar wherever you needed to display his login name. To create a tempvar just do this.

tempvars.add "Name of tempvar", value

so when you wanted to call it later it would look like this.

textbox.text = tempvars("Name of tempvar")

Or another option would be to store the actual login name of their account into the tblUser table as an extra field. You can fetch the login name but just simply using the function called CurrentUser.

So during your account creation you would need to specifify what that was initially in your datatable but from there on you could create a function to pull the data you need from the table with it.

Something like this if you called the new field LoginName.

Code:
Function ReturnUser() as text
On Error Goto ErrHandler
 
ReturnUser = Dlookup("[Fname]", "tblUser", "[LoginName] = " & CurrentUser)
Exit Function
 
ErrHandler:
msgbox err.num & " - " & err.description
ReturnUser = "Not Found"
 
End Function

Hope this helps a bit.
 
Awesome, just one quick question, where do I put the code you posted (2nd Option)? I imagine on the textbox itself, and possibly on the load event. I am not sure.

KJ
 
The code in the second option belongs in a module.

You need to bring up your visual basic editor in access and create a module. Then copy the code into there.

Now on any text box that you would like to display the current users username in you would need to set it up in the controlsource of the text box or you could use the forms onload event to set it.

If in the control just set the control source to =ReturnUser()

If in the forms on load event if the control was named txtUsername then just type this in.

me.txtUsername.value = ReturnUser

Thats it. Hopefully all is clear as mud :D
 
Thank you, I am going to try implementing that now, I will post back when I have a verdict one way or another on my abilities to follow directions...lol

KJ
 
Ok, when I test the code provided I get a Compile Error stating as follows:

User Defined Type Not Defined

Suggestions?

KJ
 
TempVars are only available in A2010 (possibly A2007). In my databases, I use my login form to hold values that I want to use throughout the session. There are a number of hidden fields on the form and after the login has been verified, I hide the form when I open the switchboard rather than closing it.

This has a big advantage over TempVars (which are not available in earlier versions of Access) and global variables (which are emptied if certain kinds of runtime errors happen). When I am testing, I can make the form visible and actually view my "globals" and even change them to test different conditions.
 
Ahh yes I see what I did wrong. Its in the where clause. Anytime your using text you have to surround it with extra quotes. My bad. Here is a working one.

Code:
Function ReturnUser() as string
On Error Goto ErrHandler
 
ReturnUser = Dlookup("[Fname]", "tblUser", "[LoginName] = """ & CurrentUser & """")
Exit Function
 
ErrHandler:
msgbox err.num & " - " & err.description
ReturnUser = "Not Found"
 
End Function

Hope it helps
 

Users who are viewing this thread

Back
Top Bottom