Pulling a username (1 Viewer)

dmyoungsal

Registered User.
Local time
Today, 01:07
Joined
May 1, 2016
Messages
112
I have partially figured out how to record a username into a table used during logging into the database.

I now want to get the contents of that field (LoginID) from the table (tblImagePath) in a textbox in another form.

Can't I just reference the field using
![tblImagePath]![LoginD]
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:07
Joined
Feb 28, 2001
Messages
27,001
Something about that question is not quite right.

Is this login something that is supposed to last for the lifetime of the Access session? If the username is in a table, is it alone? Or are there other user records? The trick is knowing which record you want when that "other form" gets involved.

One way that some folks (including me) do this is to have a general module with some support routines that look up your username and validated it against the table of valid usernames. If the general module has a public string variable set aside for the purpose, you can store the login ID in that string and then other forms can reference it, since a public variable in a general module is about as close as you can get to something being global to the application as a whole.
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:07
Joined
Jan 23, 2006
Messages
15,364
Further to Doc's comments, I'd like you to step back and describe to readers in plain, simple English---what are you trying to do?

Forget tables and syntax, just simple English as you would use to tell an 8 year old.
 

dmyoungsal

Registered User.
Local time
Today, 01:07
Joined
May 1, 2016
Messages
112
Something about that question is not quite right.

Is this login something that is supposed to last for the lifetime of the Access session? If the username is in a table, is it alone? Or are there other user records? The trick is knowing which record you want when that "other form" gets involved.

One way that some folks (including me) do this is to have a general module with some support routines that look up your username and validated it against the table of valid usernames. If the general module has a public string variable set aside for the purpose, you can store the login ID in that string and then other forms can reference it, since a public variable in a general module is about as close as you can get to something being global to the application as a whole.

In my current process, when the user opens the database, they are presented with a login screen, from which they select their name. This login determines which menu they will see while using the database. The USERID is a shortened version of their name (firstinitiallastname).

In one of my forms, (Trans Est), I want to automatically populate a field showing the person that created the Estimate.

I assume this USerID is stored someplace in the database, I just do not know where.

Before realizing the above possibility, I added a field (LoginID) to a table (tblImagePath) used in the login screen. I have successfully written the ID to the table. I can't figure out how to reference this field from my Estimate Form.

Either method would work.
 

dmyoungsal

Registered User.
Local time
Today, 01:07
Joined
May 1, 2016
Messages
112
This is what I have done so far.

I have my tblImagePath with a field LoginID

After I login to my DB and login, I open the table and see the username in the field.

I go to my form and put in =[tblImagePath]![LoginID] as the default value. when I open the form, I get the #NAME? error.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:07
Joined
Feb 28, 2001
Messages
27,001
This error is because the formula you used is not legal in VBA context.

I assume this USerID is stored someplace in the database, I just do not know where.

Actually, that's a bad assumption. If YOU didn't tell Access where to store it, it is not stored ANYWHERE. If you have ANY general subroutines in a general module, you can put variables in the declaration area (prior to the first actual routine) in order to have a place to put things that persist across forms. If you stored this info only in the login form, which then exits/closes, you do NOT have the username stored anywhere because the variables of the form's Class module vanish on form closure.
 

dmyoungsal

Registered User.
Local time
Today, 01:07
Joined
May 1, 2016
Messages
112
This error is because the formula you used is not legal in VBA context.



Actually, that's a bad assumption. If YOU didn't tell Access where to store it, it is not stored ANYWHERE. If you have ANY general subroutines in a general module, you can put variables in the declaration area (prior to the first actual routine) in order to have a place to put things that persist across forms. If you stored this info only in the login form, which then exits/closes, you do NOT have the username stored anywhere because the variables of the form's Class module vanish on form closure.

I have stored the LoginID into the table that is the basis for the login form and the name is still there even though the login form has closed.

I made the Default Value of the Textbox =[Imagepath]![LoginID]. when I open the form, I get #Name? error.

Suggestions?
 

dmyoungsal

Registered User.
Local time
Today, 01:07
Joined
May 1, 2016
Messages
112
Don't think so. I am running 2016 and have been using Environ for years.

so, what am I doing wrong? As I said, I have tried it numerous times with no luck

Just to reiterate, I am using an unbound text box and under "control source" i have entered =environ("UserName") (I have actually tried all sorts of variations of environ (with leading "=" and without)

This is the option I would like to work with, but I cannot make it work.
 

Simon_MT

Registered User.
Local time
Today, 08:07
Joined
Feb 26, 2007
Messages
2,177
Here are twi piece code:

Code:
Function GetWinUserName()
    GetWinUserName = VBA.Environ("UserName")
End Function

then

Code:
Function GetUser()
Dim db As DAO.Database
Dim rs As DAO.Recordset

    With CodeContextObject
        Set db = CurrentDb
        Set rs = db.OpenRecordset("SELECT EmployeesLookup.Employee, EmployeesLookup.[Employee Login], EmployeesLookup.[Employee Inv Approval], EmployeesLookup.[Employee Database] FROM EmployeesLookup WHERE EmployeesLookup.[Employee Login] = '" & GetWinUserName & "'")
        Do
            .UserDB = rs!Employee
            .UserInv = rs![Employee Inv Approval]
            .UserDatabase = rs![Employee Database]
            rs.MoveNext
        Loop Until rs.EOF
        rs.Close
        Set rs = Nothing
        Set db = Nothing
    End With
End Function

Simon
 

dmyoungsal

Registered User.
Local time
Today, 01:07
Joined
May 1, 2016
Messages
112
Just to give a final update, below is the code I found on this site from a post dated a few years ago. I placed the code in one of the modules loaded upon startup.

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

In the CreateBy field in my form, I put "=getWinUser" in the Default property.

Now when I open the "new" form, I get the username of the person creating a new calculation.
 

Users who are viewing this thread

Top Bottom