Solved MS access Username displayed on Textbox field

Local time
Today, 11:29
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
 
Try write in Form "On Load" event:
Code:
Private Sub Form_Load()
    Me.txtUnboundField = Environ("UserName")
End Sub
 
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 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") & "'")
 
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?"
 
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.
 
You would need to prefix that with = most likely?
 
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:
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

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

Back
Top Bottom