How do I pass a value from Form1 to Form2?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dantebothermy
    New Member
    • Nov 2008
    • 47

    How do I pass a value from Form1 to Form2?

    I have a list of patients, each of which can have multiple insurance plans. When looking at the list of patients, I want the ability to call the form to create a new insurance policy for the patients.

    The member form calls the insurance form, moves to a new record. I'd like that new record to contain the member's name (or member number) from the first form.


    This has got to be a common problem from order entry systems: you move from a customer form to a new order for the customer. How does the customer number get passed from the customer form to the order form?

    thanks


    Dante
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Just use the OpenArgs argument with the open form command.

    In the original form, behind a command button:
    Code:
    Private Sub OpenNewPolicyForm_Click()
      If Me.Dirty Then Me.Dirty = False
      DoCmd.OpenForm "NewPolicyForm", , , , , , Me.MemberID
    End Sub
    In the form to be opened:
    Code:
    Private Sub Form_Load()
      If Len(Nz(Me.OpenArgs, "")) > 0 Then
        DoCmd.GoToRecord , , acNewRec
        Me.MemberID = Me.OpenArgs
      End If
    End Sub
    Linq ;0)>

    Comment

    • dantebothermy
      New Member
      • Nov 2008
      • 47

      #3
      Linq,

      You're my newest hero.

      Thank you so much.


      Dante

      Comment

      • missinglinq
        Recognized Expert Specialist
        • Nov 2006
        • 3533

        #4
        Glad we could help!

        Linq ;0)>

        Comment

        • znyder
          New Member
          • Nov 2008
          • 5

          #5
          Originally posted by missinglinq
          Glad we could help!

          Linq ;0)>


          Hi,

          I'm new here.. and Im also following this discussion.
          I dont have much idea about programming but I do follow instruction.
          Just want to know what if I what 2 values to be pass from form1 to form2.


          thanks in advance... :)

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            Passing multiple values is a little more difficult, but doable! You don't say what you want to do with these values, so I'll just assign them to variables in the newly opened form. To assign the values, in the calling form, we'll concatenate them, placing a semi-colon between them:

            Code:
            Private Sub OpenFormPassing2Fields_Click()
              If Me.Dirty Then Me.Dirty = False
              DoCmd.OpenForm "FormToBeOpened", , , , , , Me.RecordID & ";" & Me.Rank
            End Sub
            Then, in the receiving form, we'll use the Split() function to separate the passed values and assign them to an array. We then assign the components of the array to variables. The example does this passing two values, but it could be extended to pass as many as you need to pass.

            Code:
            Private Sub Form_Load()
             If Len(Nz(Me.OpenArgs, "")) > 0 Then
                SplitArgs = Split(Me.OpenArgs, ";")
                FirstPassedField = SplitArgs(0)
                SecondPassedField = SplitArgs(1)
             End If
            End Sub
            You can now use the variables as needed, keeping in mind the scope of the variables.

            Welcome to Bytes!

            Linq ;0)>

            Comment

            • znyder
              New Member
              • Nov 2008
              • 5

              #7
              Hi.. Sorry for the late reply.. but I really appreciate your respond..
              Thanks so much.. I got it... Im really a fan of yours.. I always end up reading your post/answer to different question.. thanks again...

              znyder

              Comment

              • tangent180
                New Member
                • Oct 2009
                • 2

                #8
                Hi I have another expansion on this question I am sure you guys get once a week. I looked around quite a bit and did not see this asked anywhere. When I pass a date value It seems like openargs either crashes or sets the date to default (1899). I have tried to cast it to string and back to date a few ways but nothing seems to work. Anyone have a tip?

                Comment

                • tangent180
                  New Member
                  • Oct 2009
                  • 2

                  #9
                  Well turns out a co-worker schooled me but figured id post it here for future confused people the trick is to pre-format the date :

                  stringhold1 = Format(dateconv ert, "mm") + "/" + Format(dateconv ert, "dd") + "/" + Format(dateconv ert, "yyyy")

                  pass in argument list as :

                  "|" & stringhold1 & "|"

                  grab the value out of an open argument array and re-cast as:

                  newDate = DateValue(strCo ntrol2)

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32661

                    #10
                    Dante,

                    Linq has pretty well answered your question, but I'd just like to share a technique I use for data entry forms :
                    Instead of setting the value of any controls for a new record (which makes Access think there is a new record to create even if no other data is entered), set the .DefaultValue property of the control. This ensures the value is used if a record needs to be created, but equally allows the form to cancel the add cleanly if no data is entered.

                    Comment

                    Working...