How to display logged in user Name and Role on main form from a table (1 Viewer)

shery1995

Member
Local time
Today, 01:35
Joined
May 29, 2010
Messages
71
Hi All

I have a users table in access which holds user’s login name, password, and user type like(Administrator, data entry etc...). What I want to achieve is to display current ms access user’s login name and user type on my main. I tried (=get current user()) but it returns widow login user type like administrator. I would be thankful if someone could suggest something to achieve above.

kind regards
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:35
Joined
Oct 29, 2018
Messages
21,454
Hi. Have you tried?

=Environ("Username")
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:35
Joined
Oct 29, 2018
Messages
21,454
I am getting current date 17/11/2020
Interesting... Not sure why one function would work but another won't. Do you know how to write your own VBA function?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 19:35
Joined
Feb 28, 2001
Messages
27,147
@theDBguy - if not on a corporate-domain network then there is nothing to automatically load the environment variable "Username" when logging in. The #Name error means that "Username" cannot be found among the environment strings, not that the Environ function failed because it was not defined.

@shery1995 - the problem with debugging an app like this at home is that you are probably not in the right environment for everything to be set up properly. When you are logged in to a corporate domain, the function suggested by theDBguy would work OK to identify you. However, at home you are simply Owner.

The NORMAL way to do this if you are on a corporate network is that you have a domain. You log in to the domain and your IT staff sets up scripts on your behalf (usually, that you don't even know exist, sneaky devils that they are) to define various environmental parameters so that security software (that they also won't tell you about) can identify you if they have to report you for security transgressions.

IF you have this domain environment, then your login name (to the domain) because the Username string. In that case, you could use it to look up stuff from your Users table.

For the purposes of testing, click your Windows icon at the far left of the bottom task bar. From there select Windows System. From there, click on the Command Prompt. To the prompt which depends on your machine configuration and therefore is somewhat unpredictable by me, type

SET Username = Shery1995

Then in your test table, have a lookup name associated with "Shery1995" and you would then be able to test your code using the method suggested by theDBguy.

As an aside, developing stand-alone at home is OK if your boss says it is OK, but if you are going to start sharing your DB with others, you should really make yourself familiar with splitting a database. There are tons of articles on this forum on that subject so you should be able to find out all you need with a simple search. The SEARCH function is near the top of the page, the right-most thing on the same bar that has your forum name in it. Don't search for just "SPLIT" because there is a function by that name. Use the advanced search and seek "splitting database."
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:35
Joined
Oct 29, 2018
Messages
21,454
@theDBguy - if not on a corporate-domain network then there is nothing to automatically load the environment variable "Username" when logging in. The #Name error means that "Username" cannot be found among the environment strings, not that the Environ function failed because it was not defined.
Hi @The_Doc_Man

Thanks! That explains it. I was thinking Environ("Username") would show their login information from Windows (like your user profile under the Users folder). I may have wrongly assumed #Name refers to Access can't find the function Environ().

However, it did find the function Date(), so I started thinking it might be something to do with the Expression Service.

Thanks for the assist!
 

Gasman

Enthusiastic Amateur
Local time
Today, 01:35
Joined
Sep 21, 2011
Messages
14,238
@The_Doc_Man AFAIAA I have had a username environment variable on all my windows computer and none have been in a corporate environment.?

Edit: Thinking about it, I have only ever been the sole user of these computers, but still always had a login and a password?
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:35
Joined
Feb 19, 2002
Messages
43,233
If you are using a custom login form then instead of closing the login form when the user enters his credentials, just hid it by putting this instruction instead of closing the form.

Me.Visible = False

That way the form stays loaded and will be available from all other forms. To ghet the user's name, you have several methods.
1. if the login form is bound and the user name is included in the recordset, then just refer to the name
Me.DisplayName = Forms!frmLogin!UserName

2, if the login form doesn't include the user name, you can use dLookup()
Me.DisplayName = dLookup("UserName", "tblUsers", "UserLogin = '" & Forms!frmLogin!UserLogin & "'"

I'm assuming that the userLogin is text and so it is enclosed in quoted.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 19:35
Joined
Feb 28, 2001
Messages
27,147
@Gasman - Nothing in Windows itself sets the "Username" environment variable unless you have a login set up. But if your system was set up without at least a Windows login required at startup, then there is NOTHING to even CARE who you are. Remember that it IS possible with a stand-alone Windows system to have it come up ready-to-run as OWNER without an actual login. When first installed, Windows 10 asks if you want to create a login, but you CAN tell it "NO." As much of a PITA as Windows 10 often is, SOMETIMES it actually listens to you.

I am guessing that Shery1995 has a laptop without a login.
 

shery1995

Member
Local time
Today, 01:35
Joined
May 29, 2010
Messages
71
@Gasman - Nothing in Windows itself sets the "Username" environment variable unless you have a login set up. But if your system was set up without at least a Windows login required at startup, then there is NOTHING to even CARE who you are. Remember that it IS possible with a stand-alone Windows system to have it come up ready-to-run as OWNER without an actual login. When first installed, Windows 10 asks if you want to create a login, but you CAN tell it "NO." As much of a PITA as Windows 10 often is, SOMETIMES it actually listens to you.

I am guessing that Shery1995 has a laptop without a login.
You are absolutely spot on “ laptop without a login”
 

Users who are viewing this thread

Top Bottom