Solved MS access Username displayed on Textbox field (1 Viewer)

Local time
Today, 14:34
Joined
Jun 3, 2022
Messages
38
Hello everyone,

Is there a way to obtain the username of the Individual who’s entering information to display in a textbox or field.

For example, if Mike Thomas opened the ms access database and entered information, is there a possible way for Mike Thomas name to autopopulate in a text box

I did try the Environ(“Username”) thing on the default tab but it didnt work. Would appreciate the help. Thanks! MS access 2010 btw
 

Eugene-LS

Registered User.
Local time
Today, 22:34
Joined
Dec 7, 2018
Messages
481
Try write in Form "On Load" event:
Code:
Private Sub Form_Load()
    Me.txtUnboundField = Environ("UserName")
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:34
Joined
Oct 29, 2018
Messages
21,474
Hi. Environ("Username") should work fine. What exactly happened when you tried it? Also, did you want to store that information or just display it?
 

June7

AWF VIP
Local time
Today, 11:34
Joined
Mar 9, 2014
Messages
5,473
I did try the Environ(“Username”) thing on the default tab but it didnt work. Would appreciate the help. Thanks! MS access 2010 btw
What is "default tab"? What does "didn't work" mean - error message, wrong result, nothing happened? Do you have a Users table?

Environ("USERNAME") returns the Windows login user name. If you want to show "Mike Thomas", because I doubt Windows username is "Mike Thomas", you need a Users table storing USERNAME and real name parts.

Me.txtUnboundField = DLookup("FirstName & ' ' & LastName", "Users", "UserName='" & Environ("UserName") & "'")
 
Local time
Today, 14:34
Joined
Jun 3, 2022
Messages
38
Hi. Environ("Username") should work fine. What exactly happened when you tried it? Also, did you want to store that information or just display it?
I want the information to be stored in the table and also display on the form. I have a field in a table "user" and its in the form as well. but when i enter Environ("Username") in the textbox it shows "#Name?"
 
Local time
Today, 14:34
Joined
Jun 3, 2022
Messages
38
What is "default tab"? What does "didn't work" mean - error message, wrong result, nothing happened? Do you have a Users table?

Environ("USERNAME") returns the Windows login user name. If you want to show "Mike Thomas", because I doubt Windows username is "Mike Thomas", you need a Users table storing USERNAME and real name parts.

Me.txtUnboundField = DLookup("FirstName & ' ' & LastName", "Users", "UserName='" & Environ("UserName") & "'")
Yes, I need the Windows User to pop up in the table and textbox field.
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:34
Joined
Sep 21, 2011
Messages
14,310
You would need to prefix that with = most likely?
 

June7

AWF VIP
Local time
Today, 11:34
Joined
Mar 9, 2014
Messages
5,473
AFAIK, Access controls and queries cannot recognize Environ() function - only works in VBA. So, you can have a custom function that pulls Environ("USERNAME") and then textbox references that custom function.

Alternative to Users table is to manipulate Windows Active Directory. Review https://stackoverflow.com/questions/39205599/get-list-of-all-user-with-attributes-from-ad-with-vba

Recommend not storing USERNAME in related tables, store UserID in data records.

By "default tab" did you mean default value property?
 
Last edited:

Eugene-LS

Registered User.
Local time
Today, 22:34
Joined
Dec 7, 2018
Messages
481
I want the information to be stored in the table and also display on the form.
Take a look at example below please

Code:
Option Compare Database
Option Explicit

Private Sub Form_Load()
    Me.txtUnboundField = Environ("UserName")
End Sub

Public Function GetUserName() As String
    GetUserName = Environ("UserName")
End Function
 

Attachments

  • DatabaseUname_v01.zip
    17.6 KB · Views: 101

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:34
Joined
Feb 19, 2002
Messages
43,280
I did try the Environ(“Username”) thing on the default tab but it didnt work
Of course it works:) What was not suitable about the results?
Would CurrentUser() work in the default value? Just a thought
Did you try it? I'm guessing not.

This is what you get in the debug window.
print currentuser
Admin

Since we are not using the old Access security model anymore, users are not defined so the default will always be Admin.

If you want some user friendly name, you can get it from Active Directory. I don't have time to play with this but see where you get with it. I am getting an error on the Create Object Path line. so there must be some other library that needs a reference. Perhaps someone else knows the answer.

AccActiveDSTypeLibrary.JPG
 
Local time
Today, 14:34
Joined
Jun 3, 2022
Messages
38
Take a look at example below please

Code:
Option Compare Database
Option Explicit

Private Sub Form_Load()
    Me.txtUnboundField = Environ("UserName")
End Sub

Public Function GetUserName() As String
    GetUserName = Environ("UserName")
End Function
I was able to obtain the Windows User Name on the Form. I used this for VBA Code
Public Function GetUserName() As String
GetUserName = Environ("UserName")
End Function.

And in the MainForm Textbox User Name: =GetUserName()


But When I fill out the information in the Mainform, ->Name, hour rate, department, Username: "Windows Username, which automatically comes up" it does not save in the subform or table. Name, hour rate, department save into the subform or mainform, but not the username.
 

Eugene-LS

Registered User.
Local time
Today, 22:34
Joined
Dec 7, 2018
Messages
481
But When I fill out the information in the Mainform, ->Name, hour rate, department, Username: "Windows Username, which automatically comes up" it does not save in the subform or table. Name, hour rate, department save into the subform or mainform, but not the username.
Can you post a copy of your application with just the form (with subforms) and required tables/queries.
Just a few (fictitious) records to understand.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:34
Joined
Feb 19, 2002
Messages
43,280
@chickenwings23 "didn't work" is not helpful.

In the form's BeforeUpdate event, after all the validation is complete, you can register the user name:

Me.LastChangedByUser = Environ("UserName")
Me.LastChangedDT = Now()

That way, whenever the record gets changed, you can record who updated it and when.
 

June7

AWF VIP
Local time
Today, 11:34
Joined
Mar 9, 2014
Messages
5,473
An expression in textbox does not populate a field. Saving to field in table requires code (macro or VBA). Now I see Pat's answer and that should accommodate your requirement.
 
Last edited:

Users who are viewing this thread

Top Bottom