How do I pass a value from Form1 to Form2? | Member | | Join Date: Nov 2008
Posts: 47
| | |
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
| |
best answer - posted by missinglinq |
Just use the OpenArgs argument with the open form command.
In the original form, behind a command button: - Private Sub OpenNewPolicyForm_Click()
-
If Me.Dirty Then Me.Dirty = False
-
DoCmd.OpenForm "NewPolicyForm", , , , , , Me.MemberID
-
End Sub
-
In the form to be opened: - Private Sub Form_Load()
-
If Len(Nz(Me.OpenArgs, "")) > 0 Then
-
DoCmd.GoToRecord , , acNewRec
-
Me.MemberID = Me.OpenArgs
-
End If
-
End Sub
Linq ;0)>
|  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,000
| | | re: How do I pass a value from Form1 to Form2?
Just use the OpenArgs argument with the open form command.
In the original form, behind a command button: - Private Sub OpenNewPolicyForm_Click()
-
If Me.Dirty Then Me.Dirty = False
-
DoCmd.OpenForm "NewPolicyForm", , , , , , Me.MemberID
-
End Sub
-
In the form to be opened: - Private Sub Form_Load()
-
If Len(Nz(Me.OpenArgs, "")) > 0 Then
-
DoCmd.GoToRecord , , acNewRec
-
Me.MemberID = Me.OpenArgs
-
End If
-
End Sub
Linq ;0)>
| | Member | | Join Date: Nov 2008
Posts: 47
| | | re: How do I pass a value from Form1 to Form2?
Linq,
You're my newest hero.
Thank you so much.
Dante
|  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,000
| | | re: How do I pass a value from Form1 to Form2?
Glad we could help!
Linq ;0)>
| | Newbie | | Join Date: Nov 2008
Posts: 5
| | | re: How do I pass a value from Form1 to Form2? Quote:
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... :)
|  | Moderator | | Join Date: Nov 2006 Location: Richmond, Virginia USA
Posts: 3,000
| | | re: How do I pass a value from Form1 to Form2?
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: - 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. - 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)>
| | Newbie | | Join Date: Nov 2008
Posts: 5
| | | re: How do I pass a value from Form1 to Form2?
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
| | Newbie | | Join Date: Oct 2009
Posts: 2
| | | re: How do I pass a value from Form1 to Form2?
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?
| | Newbie | | Join Date: Oct 2009
Posts: 2
| | | re: How do I pass a value from Form1 to Form2?
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(dateconvert, "mm") + "/" + Format(dateconvert, "dd") + "/" + Format(dateconvert, "yyyy")
pass in argument list as :
"|" & stringhold1 & "|"
grab the value out of an open argument array and re-cast as:
newDate = DateValue(strControl2)
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,730
| | | re: How do I pass a value from Form1 to Form2?
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.
|  | Similar Microsoft Access / VBA bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|