user2364790
user2364790

Reputation: 73

Access passing value from one form to another

im using access 2007. My login form has username and password and i want to pass value of the username from login form to menu form. my code as below

 'Login Form'
  Private Sub Command1_Click()
If IsNull(Me.txtUserName) Then
MsgBox "Please enter Username", vbInformation, "Username Required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
'process login'
If (IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" &     Me.txtUserName.Value & "'  And password = '" & Me.txtPassword.Value & "'"))) Or _
(IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "'  And password = '" & Me.txtPassword.Value & "'"))) Then
    MsgBox "Incorrect"
Else
    DoCmd.Close       
    DoCmd.OpenForm "Menu", acNormal, Me.txtUserName     
End If
End If
End Sub

i put the code for the menu form

  Me.Label4 = "Hi " & Me.OpenArgs & "!"

i would like the menu form automatically detect what is the username. Example

 Hi John!!

The error says invalid argument. qWhere is the error in my code?

Upvotes: 0

Views: 98

Answers (3)

Andy G
Andy G

Reputation: 19367

DoCmd.OpenForm "Menu", acNormal, Me.txtUserName

OpenArgs is not the third argument, it is the seventh, which may account for the error (the third argument is a filter):

DoCmd.OpenForm "Menu", acNormal, , , , , "Bob"

(or use named arguments)

then

MsgBox "Hello " & Me.OpenArgs

will work.

Upvotes: 1

Thomas G
Thomas G

Reputation: 10216

You have 2 possibilities.

call a Public sub in your menu form

In your Menu form, create the following sub :

Public Sub Pass_Login_Info(strUserName As String)
     Me.Label4 = "Hi " & strUserName & "!"
End Sub

In the Login form, call the sub of the menu form like this :

DoCmd.OpenForm "Menu", acNormal
Form_Menu.Pass_Login_Info un

Use a Global variables to store the user info

It's the simpler and best approach to me because the Username is something that should be known and retrieved throughout all your application.

Just create a module, and in top of it add a public variable, that will be usable from anywhere

Public USER_NAME as string

In your login form :

USER_NAME = un

In the Load event of your Menu form

Private Sub Form_Load()
     Me.Label4 = "Hi " & USER_NAME & "!"         
End Sub

Upvotes: 0

MoondogsMaDawg
MoondogsMaDawg

Reputation: 1714

You can just store the username as a variable in your login form and set the message right after you open the menu:

'Login Form'
Private Sub Command1_Click()

Dim un As String

If IsNull(Me.txtUserName) Then
MsgBox "Please enter Username", vbInformation, "Username Required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else

un = Me.txtUserName

'process login'
If (IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" &     Me.txtUserName.Value & "'  And password = '" & Me.txtPassword.Value & "'"))) Or _
(IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "'  And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect"
Else
DoCmd.Close       
DoCmd.OpenForm "Menu", acNormal, Me.txtUserName

Forms!Menu!Label4 = "Hi " & un & "!"

End If
End If
End Sub

If it throws an error, move the DoCmd.Close to the end.

Upvotes: 0

Related Questions