Add Window Username to Table (1 Viewer)

sefa333

Registered User.
Local time
Today, 13:52
Joined
Nov 17, 2011
Messages
19
Thanks in advance for your help. I am still new to Access code.
I was wanting to know if there is a way to Add the Username to a table as a default value when a new record is added. I know you can add =Date() to get the date. Is there a simple way to get the ID of the person logged into Window? Thanks again.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 06:52
Joined
Jan 20, 2009
Messages
12,859
Copy this function into a Standard Module.
Code:
Public Function LoginName()
    LoginName = CreateObject("wscript.network").UserName
End Function

Use the function as the default value of a textbox on the form bound to the field in the table where you want to write the username. The textbox can be hidden.
 

sefa333

Registered User.
Local time
Today, 13:52
Joined
Nov 17, 2011
Messages
19
Thank you very Much. It works great!!
 

basshead22

Registered User.
Local time
Today, 13:52
Joined
Dec 17, 2013
Messages
52
what would be the default value for my text box?

Whatever I name the module? I tried giving the module "username" but when I do that in the default value it thinks it a table or field and puts it in brackets.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 06:52
Joined
Jan 20, 2009
Messages
12,859
The module can be pretty much any name except the name of a sub/function or the project name.

The module name is not used in the reference. Just the function name.
 

Matoco1

Registered User.
Local time
Today, 21:52
Joined
Jan 5, 2009
Messages
32
I have been using =Environ("username") for this.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 06:52
Joined
Jan 20, 2009
Messages
12,859
I have been using =Environ("username") for this.

So do a lot of other developers. However it isn't a very good choice because environment variables can be tampered with.

Better to use the actual username from the system.
 

Users who are viewing this thread

Top Bottom