How to pass value from form 1 to form 2

kimchilee

New member
Local time
Today, 01:41
Joined
Jul 13, 2011
Messages
5
I am building a database for a school project. The database contains forms frmDemographics and frmHealthInfo and they are linked to tblDemographics and tblHealthInfo, respectively. tblDemographics store patients information while tblHealthInfo store information about how they search for health related information. Both tables have subjectID as the primary key and it links these two tables together. I would like to add new patient in frmDemographics and when I go to frmHealthInfo, I would like SubjectID from frmDemogrphics to be passed to frmHealthInfo automatically. How can this be done? I tried to add following code under Onload and it didn't work:
Me.SubjectID = Forms!frmDemographics!SubjectID
 
You did not indicate where you have your current line of code:
Code:
Me.SubjectID = Forms!frmDemographics!SubjectID

If you have this line of code in the OnCurrent event of your frmHealthInfo form and you are addressing the actual names of the respective controls, it should work.

One other way you can pass a value from one form to another is to use the OpenArgs parameter when using the OpenForm method.

You would need to have code in the event that is currently opening the frmHealthInfo form. The code below assumes that the name of the control that has the primary key value from the record in the frmDemographics form.
Code:
DoCmd.OpenForm "frmHealthInfo", , , , , , Me.SubjectID

The you will need code in the OnOpen or OnCurrent event of the frmHealthInfo form that will assign the value passed in the OpenArgs parameter to the appropriate control. Assuming that the name of the control in the frmHealthInfo form that you want to assign the primary key value to is "SubjectID", you would have code like:
Code:
Me.SubjectID = Me.OpenArgs

Either way will work just fine.
 
In the OnOpen event of the second form you'll need to move to a new Record before assigning the SubjectID from the first Form, otherwise it will be assigned to the first Record in the second Form, which isn't your intent!

Likewise, placing the code in the OnCurrent event of the second Form will assign the SubjectID to any Record you view, also not your intent!

The way this type of situation would be handled by most professionals would be to have frmDemographics as a Main Form, in Single View, and frmHealthInfo as a Subform. You'd link the two by the SubjectID Field and the Control on Subform would be automatically filled in by Access. You could fill both at the same time.

Linq ;0)>
 
hello, I am doing something similar to this. How do you move to a new record in the On Open event?
 
Mr. B,
Thank you for your help. The value is now passed from frmDemographics to frmHealthInfo but they do not get stored in the tables. What should I do?
 
The SubjectID Control on frmHealthInfo has to be bound to a Field in tblHealthInfo in order for the passed data to be stored.

Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom