Full Version: Pass Data Chosen On Anoter Form To Text Box On Current Form
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
gloworm
When this database is run, it has a startup macro for a logon form.
Once you log in, it takes you to a Menu form.

On this menu form, it has a welcome message and should display the username of the person that logged into the database.
This is not the case.
I am getting a #Name? error.

This text box originally had =Environ("Username") as the expression.
I did some reading and found that it was not a preferred way.

So I started poking around, trying different expressions.
I had it equal the field that the logon form refers to, didnt work.
I told it to equal the combo box on the logon form, it didnt work either.


How would I get this text box to equal the name of the person who logged into the database?
There are dates and time fields that are working perfectly fine.
merlenicholson
I don't see anything wrong with Environ("UserName"), but what you want is available from the Windows API. We have to start being careful with Windows API now, because there's a different set of them when using the 64-bit version of Access. This issue has been addressed many times in UtterAccess, just use the Search for "Logon Name" and you'll get a lot of hits including this very good one. Logon Name
theDBguy
Hi,

If you are referring to the user's network login username, then as Merle pointed out, the Environ() function should work but the API method is preferred.

Otherwise, if you are using a table for the login name, I would recommend either hiding the login form instead of closing it, or set up a TempVar to hold that information.

Either way should give you access to the login name wherever you need to in your database.

Just my 2 cents... 2cents.gif
gloworm
QUOTE (merlenicholson @ May 18 2012, 11:03 AM) *
I don't see anything wrong with Environ("UserName"), but what you want is available from the Windows API. We have to start being careful with Windows API now, because there's a different set of them when using the 64-bit version of Access. This issue has been addressed many times in UtterAccess, just use the Search for "Logon Name" and you'll get a lot of hits including this very good one. Logon Name



That gave ADMIN as the result when I dropped it on the form.
=CurrentUser()

I want the actual name that was chosen when the database was logged on to.
gloworm
QUOTE (theDBguy @ May 18 2012, 11:06 AM) *
Hi,

If you are referring to the user's network login username, then as Merle pointed out, the Environ() function should work but the API method is preferred.

Otherwise, if you are using a table for the login name, I would recommend either hiding the login form instead of closing it, or set up a TempVar to hold that information.

Either way should give you access to the login name wherever you need to in your database.

Just my 2 cents... 2cents.gif



How do I do that?
I want the name chosen when the database was logged on to.
theDBguy
QUOTE (gloworm @ May 18 2012, 09:37 AM) *
How do I do that?
I want the name chosen when the database was logged on to.

Are they choosing the name from a dropdown box? If so, you can hide the login form and then refer to it using the following syntax:

Forms!FormName.ControlName

For example, if the name of the form is "Login" and the name of the dropdown box is "Username," then use this:

=Forms!Login.Username

To hide the login form, just set its Visible property to False. For example:

Forms!Login.Visible = False

Just my 2 cents... 2cents.gif
gloworm
QUOTE (theDBguy @ May 18 2012, 12:11 PM) *
Are they choosing the name from a dropdown box? If so, you can hide the login form and then refer to it using the following syntax:

Forms!FormName.ControlName

For example, if the name of the form is "Login" and the name of the dropdown box is "Username," then use this:

=Forms!Login.Username

To hide the login form, just set its Visible property to False. For example:

Forms!Login.Visible = False

Just my 2 cents... 2cents.gif



Why would I want to hide the login form?
They have to login.
I could be on Betty Boops machine, but still login as myself in the database.
In that case, I would want to see "Welcome, [My Name]"
theDBguy
QUOTE (gloworm @ May 18 2012, 10:18 AM) *
Why would I want to hide the login form?
They have to login.

I mean, after they logged in. I assumed that you are closing the login form after they have logged in, so all I'm saying is keep it open but hidden, so you can grab the log in name anytime you need it.

Makes sense?
gloworm
QUOTE (theDBguy @ May 18 2012, 12:11 PM) *
Are they choosing the name from a dropdown box? If so, you can hide the login form and then refer to it using the following syntax:

Forms!FormName.ControlName

For example, if the name of the form is "Login" and the name of the dropdown box is "Username," then use this:

=Forms!Login.Username

To hide the login form, just set its Visible property to False. For example:

Forms!Login.Visible = False

Just my 2 cents... 2cents.gif



I am trying to figure out how to do this.
I cant even figure out what is running when the database opens.
There is no autoexec, but the login form comes up as soon as the database opens.

Where can i find it if I have no autoexec to look at?


What do I do when I find it, add it as another part of the macro that opens up hidden?
If I do it like that, how would it know what name was chosen if it is opened as a different hidden form?
Bob G
QUOTE
When this database is run, it has a startup macro for a logon form.


You need to see what this macro does.
gloworm
QUOTE (Bob G @ May 18 2012, 12:42 PM) *
You need to see what this macro does.



I do not find any macro that opens this form.

I have opened each and every one of them and do not see any of them that open the login form.
theDBguy
Hi,

Please describe to us what happens after the user logs in. Or, give us a step-by-step of what the user will be doing when they use this database, from opening it, to logging in, to using it.

Just my 2 cents... 2cents.gif
gloworm
I figured the logon form opening issue.
Someone added it to the database options.
In there you can specify a form to open when the database opens.

Now, since it is not done with a macro, should I create the autoexec macro and remove the login form from the options?

If the answer is yes, create the autoexec, do I add the form to the macro twice?
Once as visible and once as hidden?



DB is opened.
Logon form comes up.
user hits dropdown and chooses their name.
Enters password and hits logon button.

If the logon was successful, a form with a menu comes up for the user to navigate with.



I am coming in on this project alone and very deep in to it.
They let the other person go last firday and they had no documentation for me to go on.
It also appears as though the last person here was in the middle of changing some stuff and it is only half way done.
I cant even find a backup from before she started to implement these changes that are half done.
theDBguy
QUOTE (gloworm @ May 18 2012, 11:00 AM) *
DB is opened.
Logon form comes up.
user hits dropdown and chooses their name.
Enters password and hits logon button.

If the logon was successful, a form with a menu comes up for the user to navigate with.

Okay, so in the code behind the "logon button," you can add the part where you would hide the logon form before opening the menu form. Then, in the menu form (or anywhere else in your database), you can always get the logon name by using the Forms syntax I showed you earlier.

Just my 2 cents... 2cents.gif
gloworm
QUOTE (theDBguy @ May 18 2012, 01:03 PM) *
Okay, so in the code behind the "logon button," you can add the part where you would hide the logon form before opening the menu form. Then, in the menu form (or anywhere else in your database), you can always get the logon name by using the Forms syntax I showed you earlier.

Just my 2 cents... 2cents.gif



Add it where?

Here is the code behind the button:

Private Sub cmdLogin_Click()

'Check to see if data is entered into the UserName combo box

If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboEmployee.SetFocus
Exit Sub
End If

'Check to see if data is entered into the password box

If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If

'Check value of password in tblEmployees to see if this
'matches value chosen in combo box

If Me.txtPassword.Value = DLookup("strEmpPassword", "tblEmployees", _
"[lngEmpID]=" & Me.cboEmployee.Value) Then

lngMyEmpID = Me.cboEmployee.Value

'Close logon form and open splash screen

DoCmd.Close acForm, "frmLogon", acSaveNo
DoCmd.OpenForm "frmMenu"

Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
"Invalid Entry!"
Me.txtPassword.SetFocus
End If

'If User Enters incorrect password 3 times database will shutdown

intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If

End Sub
theDBguy
QUOTE (gloworm @ May 18 2012, 11:07 AM) *
Add it where?

In here...

...
'Close logon form and open splash screen

'DoCmd.Close acForm, "frmLogon", acSaveNo

'Hide logon form and open splash screen
Me.Visible = False

DoCmd.OpenForm "frmMenu"
...


Just my 2 cents... 2cents.gif
gloworm
QUOTE (theDBguy @ May 18 2012, 01:13 PM) *
In here...

...
'Close logon form and open splash screen

'DoCmd.Close acForm, "frmLogon", acSaveNo

'Hide logon form and open splash screen
Me.Visible = False

DoCmd.OpenForm "frmMenu"
...


Just my 2 cents... 2cents.gif



Didn't work.

I now get the following:

Run Time error 5
Invalid Procedure Call or Argument



When I tell it debug, it takes me right to the visible part I added.
gloworm
I got past the error.
I am guessing at everything I am doing.

When the Menu form comes up, it now says Welcome 4.

Not the name of the person.


The Combo Box that is the dropdown to choose name in, has a row source that is a query.
This query pulls EmpID and Name.

How do I get the name portion to come through?
theDBguy
QUOTE (gloworm @ May 18 2012, 11:29 AM) *
When the Menu form comes up, it now says Welcome 4.
...
What now?

You can use the Column property of the Combobox. For example:

=Forms!frmLogon.cboEmployee.Column(1)

Just my 2 cents... 2cents.gif
gloworm
QUOTE (theDBguy @ May 18 2012, 01:32 PM) *
You can use the Column property of the Combobox. For example:

=Forms!frmLogon.cboEmployee.Column(1)

Just my 2 cents... 2cents.gif



Guess what???????????
I cant figure it out again.

Here is what I have in the control source where I want the name to come through:

=[Forms]![frmLogon]![cboEmployee]![strEmpName]

I even tried column(2).

theDBguy
Hi,

Please post the SQL for the combobox on the login form. Thanks.
gloworm
QUOTE (theDBguy @ May 18 2012, 01:44 PM) *
Hi,

Please post the SQL for the combobox on the login form. Thanks.




SELECT tblEmployees.lngEmpID, tblEmployees.strEmpName FROM tblEmployees ORDER BY [strEmpName];
theDBguy
QUOTE (gloworm @ May 18 2012, 11:45 AM) *
SELECT tblEmployees.lngEmpID, tblEmployees.strEmpName FROM tblEmployees ORDER BY [strEmpName];

So, I was right, you're supposed to use the syntax I gave you earlier as it is:

=Forms!frmLogon.cboEmployee.Column(1)

What do you get with that?
gloworm
I get #error


theDBguy
QUOTE (gloworm @ May 18 2012, 11:49 AM) *
I get #error

This is with the form hidden, correct? You did notice that I commented out the line in your code that closes it, right?
gloworm
QUOTE (theDBguy @ May 18 2012, 01:55 PM) *
This is with the form hidden, correct? You did notice that I commented out the line in your code that closes it, right?



Still doesnt work.

Here is current code behind Logon button:

Private Sub cmdLogin_Click()

'Check to see if data is entered into the UserName combo box

If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboEmployee.SetFocus
Exit Sub
End If

'Check to see if data is entered into the password box

If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If

'Check value of password in tblEmployees to see if this
'matches value chosen in combo box

If Me.txtPassword.Value = DLookup("strEmpPassword", "tblEmployees", _
"[lngEmpID]=" & Me.cboEmployee.Value) Then

lngMyEmpID = Me.cboEmployee.Value

'Close logon form and open splash screen

' DoCmd.Close acForm, "frmLogon", acSaveNo

'Hide logon form and open splash screen
Me.Visible = False

DoCmd.OpenForm "frmMenu"

Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
"Invalid Entry!"
Me.txtPassword.SetFocus
End If

'If User Enters incorrect password 3 times database will shutdown

intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If

End Sub



Here is what I have for the Control Source on Menu Form for name:

=[Forms]![frmLogon].[cboEmployee].[Column(1)]




theDBguy
Hi,

Earlier, you said that there was a problem with the Visible code and you were able to fix it. What exactly was the problem and how did you fix it? That could be affecting what's going on right now.

Also, I notice in your code that you may have a global variable in your database that you could also use for this. Try the following in your textbox:

=DLookup("strEmpName", "tblEmployees", "lngEmpID=" & lngMyEmpID)

Just my 2 cents... 2cents.gif
gloworm
When I use:
=[Forms]![frmLogon].[cboEmployee].[Column1]

I get #NAME?


When I use:
=[Forms]![frmLogon].[cboEmployee].[Column(1)]

I get #NAME?



When I use:
=[Forms]![frmLogon].[cboEmployee].[Column](1)

I get #Error



When I use:
=[Forms]![frmLogon]![cboEmployee]

I get 4, which is the value in the first column of the query.
I want the name that comes in the second column
theDBguy
Hi,

There's really no need to try any other syntax. The one I gave you should work.

Can you post the following properties for your Combobox?

Column Count
Bound Column
Column Widths

Thanks!
gloworm
QUOTE (theDBguy @ May 18 2012, 02:15 PM) *
Hi,

Earlier, you said that there was a problem with the Visible code and you were able to fix it. What exactly was the problem and how did you fix it? That could be affecting what's going on right now.

Also, I notice in your code that you may have a global variable in your database that you could also use for this. Try the following in your textbox:

=DLookup("strEmpName", "tblEmployees", "lngEmpID=" & lngMyEmpID)

Just my 2 cents... 2cents.gif



I get #NAME?
When I add it to the textbox.

theDBguy
QUOTE (gloworm @ May 18 2012, 12:32 PM) *
I get #NAME?
When I add it to the textbox.

Okay, all these workarounds shouldn't be necessary but try this one now:

=DLookup("strEmpName", "tblEmployees", "lngEmpID=" & [Forms]![frmLogon]![cboEmployee])

Just my 2 cents...
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.