Connecting Tech Pros Worldwide Forums | Help | Site Map

How do I pass a value from Form1 to Form2?

Member
 
Join Date: Nov 2008
Posts: 47
#1: Nov 6 '08
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub OpenNewPolicyForm_Click()
  2.   If Me.Dirty Then Me.Dirty = False
  3.   DoCmd.OpenForm "NewPolicyForm", , , , , , Me.MemberID
  4. End Sub
  5.  
In the form to be opened:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.   If Len(Nz(Me.OpenArgs, "")) > 0 Then
  3.     DoCmd.GoToRecord , , acNewRec
  4.     Me.MemberID = Me.OpenArgs
  5.   End If
  6. End Sub
Linq ;0)>

missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#2: Nov 6 '08

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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub OpenNewPolicyForm_Click()
  2.   If Me.Dirty Then Me.Dirty = False
  3.   DoCmd.OpenForm "NewPolicyForm", , , , , , Me.MemberID
  4. End Sub
  5.  
In the form to be opened:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.   If Len(Nz(Me.OpenArgs, "")) > 0 Then
  3.     DoCmd.GoToRecord , , acNewRec
  4.     Me.MemberID = Me.OpenArgs
  5.   End If
  6. End Sub
Linq ;0)>
Member
 
Join Date: Nov 2008
Posts: 47
#3: Nov 6 '08

re: How do I pass a value from Form1 to Form2?


Linq,

You're my newest hero.

Thank you so much.


Dante
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#4: Nov 6 '08

re: How do I pass a value from Form1 to Form2?


Glad we could help!

Linq ;0)>
Newbie
 
Join Date: Nov 2008
Posts: 5
#5: Nov 11 '08

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... :)
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#6: Nov 11 '08

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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub OpenFormPassing2Fields_Click()
  2.   If Me.Dirty Then Me.Dirty = False
  3.   DoCmd.OpenForm "FormToBeOpened", , , , , , Me.RecordID & ";" & Me.Rank
  4. 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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  If Len(Nz(Me.OpenArgs, "")) > 0 Then
  3.     SplitArgs = Split(Me.OpenArgs, ";")
  4.     FirstPassedField = SplitArgs(0)
  5.     SecondPassedField = SplitArgs(1)
  6.  End If
  7. 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
#7: Dec 4 '08

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
#8: Oct 19 '09

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
#9: Oct 19 '09

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)
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#10: Oct 20 '09

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.
Reply