Transferring the value of a form's text control to another form's text control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jesse Jones
    New Member
    • Jan 2011
    • 51

    Transferring the value of a form's text control to another form's text control

    I have a login form that I am using to direct particular people to a customized switchboard. All of the login process is working perfectly. (I am not doing this for security but for simplicity, so there is no concern about the faults of VBA for security.)

    What I need is to get an unbound text control on the formSwitchboard to lookup the EID that was entered on the login form. How can I do this?

    Thank you in advance!
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32661

    #2
    When you open the Switchboard form, use the OpenArgs parameter to pass the value across.

    The details of how to do this are covered in a very recent thread (How to pass a variable from one form to another form).

    Comment

    • Jesse Jones
      New Member
      • Jan 2011
      • 51

      #3
      Thank you for the link. I'm sorry, but I am a complete programming newbie, really. Just a few months experience with any programming of any kind.

      What I am trying to do is pass the value that is in the control "txtEID" on formLogin to a control "txtEIDGrab " on fromSwitchboard . formSwitchboard opens after the credentials are verified through a VBA sequence that runs on a command click on formLogin. If the credentials are legitimate, the proper switchboard is opened.

      Code you please help me with the code that should run on open of the formSwitchboard ?

      Thank you!

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        We don't often respond well to simple requests for code, but as you asked nicely and had the intelligence to provide the names of the objects to work with I'm sure we can make a little exception here.

        It's only short, but try :
        Code:
        Private Sub Form_Open(Cancel As Integer)
            Me.txtEIDGrab = Me.OpenArgs
        End Sub

        Comment

        • Jesse Jones
          New Member
          • Jan 2011
          • 51

          #5
          Thank you for your help. I'm sorry I'm so ignorant. I must be missing something because that string doesn't work, and I'm honestly not sure how it could (because I don't understand it). Here is the total code that runs on click of the login command button on formLogin.

          Code:
          Private Sub cmmndLogIn_Click()
          'Ensure an EID is entered
              If Me.txteid = "" Or IsNull(Me.txteid) Then
                  MsgBox "You must enter a valid EID and password", vbOKOnly, "Required Data!"
                  Me.txteid = ""
                  Me.txtPassword = ""
                  Me.txteid.SetFocus
                  Exit Sub
              End If
          
          'Ensure the EID is for an active employee
              If Me.txtEmploymentStatusLookup <> "Active" Then
                  MsgBox "You must enter a valid EID and password", vbOKOnly, "Required Data!"
                  Me.txteid = ""
                  Me.txtPassword = ""
                  Me.txteid.SetFocus
                  Exit Sub
              End If
              
          'Ensure a password is entered
              If Me.txtPassword = "" Or IsNull(Me.txtPassword) Then
                  MsgBox "You must enter a valied EID and password", vbOKOnly, "required Data!"
                  Me.txteid = ""
                  Me.txtPassword = ""
                  Me.txteid.SetFocus
                  Exit Sub
              End If
          
          'Ensure the password is correct
              If Me.txtPassword <> Me.txtPasswordLookUp Then
                  MsgBox "You must enter a valid EID and password", vbOKOnly, "Required Data!"
                  Me.txteid = ""
                  Me.txtPassword = ""
                  Me.txteid.SetFocus
                  Exit Sub
              End If
          
          'Open formSwitchboard
              DoCmd.OpenForm "formSwitchboard"
          
          
          End Sub
          (I will add the code deciding which switchboard the user is directed to later.)

          Here is the on open event for formSwitchboard

          Code:
          Private Sub Form_Open(Cancel As Integer)
          'Grab the user's EID from formLogin and enter it into txtEIDGrab
          Me.txtEIDGrab = Me.OpenArgs
          
          End Sub
          It looks (from some of my internet research) that I'm supposed to mark in the first form what is to be carried to the other form, but I haven't a clue how to do it.

          I'm sorry again. Any hints would be greatly appreciated.

          Comment

          • Mihail
            Contributor
            • Apr 2011
            • 759

            #6
            If you have an BOUND text box (the text box refer a field in a record in a table or query) the text box value can be NULL but, as far as I know, can not be "" . So, in this case, should work this code:
            Code:
            If IsNull(txtPassword) Then ....
            If you have an UNBOUND text box, the text box value can be "" but can not be NULL. So, in this case, should work this code:
            Code:
            If txtPassword = "" Then ....
            Also note that the keyword Me is unnecessary.

            Hope this is a help for you.
            Good luck !

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32661

              #7
              Originally posted by Mihail
              Mihail:
              Also note that the keyword Me is unnecessary.
              This is very true (although there can be exceptions to this where field names clash with control names), but many people nevertheless feel it is worth including for the sake of clarity. It gives a clue to any reader of the code what type of object to look for. This can be particularly helpful within large and complex databases/projects.

              The other point (Controls <> "") is a good one, and I will illustrate this in my next post for Jesse.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32661

                #8
                Originally posted by Jesse Jones
                Jesse Jones:
                Code you please help me with the code that should run on open of the formSwitchboard ?
                I'm happy to help with that too Jesse, but your earlier post asked specifically for the code in the switchboard form (which I duly provided).

                If you look at line #39 of the code you posted for your cmmndLogIn_Clic k() procedure, you'll see that it doesn't include anything for the OpenArgs parameter. This needs to be added, but there are various ways of doing this. I'll use named parameters as I find positional parameters harder to read, especially when any are blank :
                Code:
                Call DoCmd.OpenForm(FormName:="formSwitchboard", OpenArgs:=Me.txteid)
                As a bonus, and to illustrate the good point Mihail made, I'll post here an example of how you could write your cmmndLogIn_Clic k() procedure a little more succinctly :
                Code:
                Private Sub cmmndLogIn_Click()
                Dim strMsg As String
                Dim ctlMe As Control
                
                With Me
                    Select Case True
                    Case IsNull(.txteid)
                        'Ensure an EID is entered
                        strMsg = "You must enter an EID and a password"
                        Set ctlMe = .txteid
                    Case IsNull(.txtPassword)
                        'Ensure a password is entered
                        strMsg = "You must enter an EID and a password"
                        Set ctlMe = .txtPassword
                    Case .txtEmploymentStatusLookup <> "Active"
                        'Ensure the EID is for an active employee
                        strMsg = "Employee status must be 'Active'"
                        Set ctlMe = .txteid
                    Case .txtPassword <> .txtPasswordLookUp 
                        'Ensure the password is correct
                        strMsg = "The password entered must be valid"
                        Set ctlMe = .txtPassword
                    End Select
                
                    If strMsg > "" Then
                        With ctlMe
                            .Value = Null
                            Call ctlMe.SetFocus
                        End With
                        Call MsgBox(strMsg, vbOKOnly Or vbExclamation, "Required Data!"
                        Exit Sub
                    End If
                
                    'Open formSwitchboard
                    Call DoCmd.OpenForm(FormName:="formSwitchboard", OpenArgs:=.txteid)
                End With
                End Sub

                Comment

                • Jesse Jones
                  New Member
                  • Jan 2011
                  • 51

                  #9
                  Thank you both so much. You have been very, very helpful. Everything is working great!

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32661

                    #10
                    Good for you Jesse. Pleased to hear it went well :-)

                    Comment

                    • Mihail
                      Contributor
                      • Apr 2011
                      • 759

                      #11
                      Thank you, NeoPa !

                      Comment

                      • Jesse Jones
                        New Member
                        • Jan 2011
                        • 51

                        #12
                        Is it possible to send two different controls through?

                        I would like txtDate on frmAttendanceDa ta to pass through to txtGroupDate on frmAttendanceEn ter

                        and cmbEvent on frmAttendanceDa ta to pass through to txtGroupEvent on frmAttendanceEn ter.

                        Is that possible?

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32661

                          #13
                          You have a single value to pass through to the new form. In most cases it's easy to see how you could include both the required values in a single parameter. I'd use a string with a separator character myself.

                          If you wanted to pass two numeric values for instance, you might pass them as (Assuming values of 3 & 45) "3;45". The receiving code would need to separate them back out again of course. Split() is a good function for that sort of thing.

                          Comment

                          Working...