Pass info from one form to another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jollywg
    New Member
    • Mar 2008
    • 158

    Pass info from one form to another

    I've got a simple question, how do i pass information (lets say a name that is input in a textbox) to another form

    Thanks
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Depends on how you want to pass it. If you want to pass it from, say, FormA to FormB, when opening FormB, the best way would to use OpenArgs.

    In your calling form:

    Code:
    Private Sub OpenFormB_Click()
      DoCmd.OpenForm "FormB", , , , , , Me.FieldToPass    
    End Sub
    Then, in FormB:

    Code:
    Private Sub Form_Load()
      If Len(Nz(Me.OpenArgs, "")) > 0 Then
    	Me.WhateverField = Me.OpenArgs
      End If
    End Sub
    If both forms are already open, you could assign the data to pass to a global variable, or simply explicitly reference it as a control on the first form.

    Linq ;0)>

    Comment

    • Jollywg
      New Member
      • Mar 2008
      • 158

      #3
      [QUOTE=missingli nq]Depends on how you want to pass it. If you want to pass it from, say, FormA to FormB, when opening FormB, the best way would to use OpenArgs.

      In your calling form:

      Code:
      Private Sub OpenFormB_Click()
        DoCmd.OpenForm "FormB", , , , , , Me.FieldToPass    
      End Sub
      Then, in FormB:

      Code:
      Private Sub Form_Load()
        If Len(Nz(Me.OpenArgs, "")) > 0 Then
      	Me.WhateverField = Me.OpenArgs
        End If
      End Sub
      If both forms are already open, you could assign the data to pass to a global variable, or simply explicitly reference it as a control on the first form.

      Linq ;0)>[/QUOTE

      Thank you so much, i think that will work, but is there some way to pass multiple values such as Me.txtFirstname , Me.txtLastname? ?

      Thanks

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        1. You can pass as many Values as you like in the OpenArgs Argument of the OpenForm() Method as long as you use a Common Delimiter between then.
        2. In the following example, I'll pass 4 Values (Argument 1 to Argument 4) to Form2 via the OpenArgs Argument of the OpenForm() Method:
          [CODE=vb]
          DoCmd.OpenForm "Form2", acNormal, , , acFormEdit, acWindowNormal, "Argument 1,Argument 2,Argument 3,Argument 4"[/CODE]
        3. In the receiving Form, you can use the Split() Function to retrieve the variable number of Values:
          [CODE=vb]
          Dim varOpenArgs As Variant
          Dim intCounter As Integer

          varOpenArgs = Split(Me.OpenAr gs, ",")

          For intCounter = LBound(varOpenA rgs) To UBound(varOpenA rgs)
          Debug.Print varOpenArgs(int Counter)
          Next[/CODE]

        OUTPUT:
        [CODE=text]
        Argument 1
        Argument 2
        Argument 3
        Argument 4[/CODE]

        Comment

        • Jollywg
          New Member
          • Mar 2008
          • 158

          #5
          Thanks folks both of those posts worked like a charm, but i have one more problem that i ran into.
          I need to pass the current primary key value, which is displayed in a textbox, from form1 which is made off of table Patient to form2 which is made off of table IV. Table Patient and Table IV are linked in a one to many relationship and the joining column is PatientID.

          I currently have PatientID on both forms, and when i pass it the way you all showed me the status bar on form2 says "Control can't be edited; it's bound to AutoNumber field 'PatientID'. I get what this is saying, but i dont know how to "work around" it.

          Ideally what this should do is in form1 the user is viewing a patient (with a unique PatientID) they click the "NewIV" button and access opens up form2 and lets the user add a new IV to the patientID that was passed to it.

          I really appreciate all the help Bytes has given me over the course of this project. Thanks for any responses

          Matt

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by Jollywg
            Thanks folks both of those posts worked like a charm, but i have one more problem that i ran into.
            I need to pass the current primary key value, which is displayed in a textbox, from form1 which is made off of table Patient to form2 which is made off of table IV. Table Patient and Table IV are linked in a one to many relationship and the joining column is PatientID.

            I currently have PatientID on both forms, and when i pass it the way you all showed me the status bar on form2 says "Control can't be edited; it's bound to AutoNumber field 'PatientID'. I get what this is saying, but i dont know how to "work around" it.

            Ideally what this should do is in form1 the user is viewing a patient (with a unique PatientID) they click the "NewIV" button and access opens up form2 and lets the user add a new IV to the patientID that was passed to it.

            I really appreciate all the help Bytes has given me over the course of this project. Thanks for any responses

            Matt
            It appears that what you need is to use the Where Argument of the OpenForm() Method and not OpenArgs, something like:
            [CODE=vb]
            DoCmd.OpenForm "Form2", acNormal, , "[PatientID] = " & Me![PatientID], acFormEdit[/CODE]

            Comment

            • FishVal
              Recognized Expert Specialist
              • Jun 2007
              • 2656

              #7
              Hi, Matt.

              I guess you may get some ideas from the following threads:
              Add Matching Record on Filtered Form
              Datasheet to Open Child Form

              Regards,
              Fish

              Comment

              Working...