DoCmd.OpenForm opens empty subform

J

jaworski_m

I use the following code in the OnClick event to open subform (variables
declared) with specific record(s).

---- CODE START ----
stDocName = "sub_Form_Name"

stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
---- CODE END -----
1) If records in the sub form exist - it opens OK, corresponding records are
shown.
2) If there are NO records, it opens BLANK and the ID field,i.e.Primary Key,
contains NULL value - adding new record has no sense, because there is no
link between the related tables.

QUESTION: What's the way to open the sub form with the above code so the
Primary Key value is populated to the "many table"?

Thank you for suggestions.
 
J

John W. Vinson

I use the following code in the OnClick event to open subform (variables
declared) with specific record(s).

---- CODE START ----
stDocName = "sub_Form_Name"

stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
---- CODE END -----
1) If records in the sub form exist - it opens OK, corresponding records are
shown.
2) If there are NO records, it opens BLANK and the ID field,i.e.Primary Key,
contains NULL value - adding new record has no sense, because there is no
link between the related tables.

QUESTION: What's the way to open the sub form with the above code so the
Primary Key value is populated to the "many table"?

Thank you for suggestions.

I would really suggest that you actually use a Subform control on the main
form. Simply drag the subform onto the main form in design view and set its
Master/Child Link Field to the ID. If you do so you'll need no code at all,
and won't need to "open" the subform, it'll just be there.

If you have some reason to use the more complex process of popping up a form
and still keeping it synchronized, you can... post back with an explanation of
why you want to do it the hard way!
 
J

jaworski_m

Thank you for reply.

Well, I already have a subform control on the main form and by means of
using a button to open a child table/form I could save space required for
other controls (text control...)

John W. Vinson said:
I use the following code in the OnClick event to open subform (variables
declared) with specific record(s).

---- CODE START ----
stDocName = "sub_Form_Name"

stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
---- CODE END -----
1) If records in the sub form exist - it opens OK, corresponding records are
shown.
2) If there are NO records, it opens BLANK and the ID field,i.e.Primary Key,
contains NULL value - adding new record has no sense, because there is no
link between the related tables.

QUESTION: What's the way to open the sub form with the above code so the
Primary Key value is populated to the "many table"?

Thank you for suggestions.

I would really suggest that you actually use a Subform control on the main
form. Simply drag the subform onto the main form in design view and set its
Master/Child Link Field to the ID. If you do so you'll need no code at all,
and won't need to "open" the subform, it'll just be there.

If you have some reason to use the more complex process of popping up a form
and still keeping it synchronized, you can... post back with an explanation of
why you want to do it the hard way!
 
R

Rick Brandt

Thank you for reply.

Well, I already have a subform control on the main form and by means of
using a button to open a child table/form I could save space required
for other controls (text control...)

Using a TabControl would eliminate that issue.
 
J

John W. Vinson

Thank you for reply.

Well, I already have a subform control on the main form and by means of
using a button to open a child table/form I could save space required for
other controls (text control...)

As Rick says, you could manage screen real estate by using a Tab Control: put
your main form's textboxes, combos, etc. on one page of the tab control, and
the subform on a second page. I'll do this routinely on complex forms.

If you do want the popup feature, you will need to pass the linking ID in
*both* the WhereCondition argument and the OpenArgs argument of the OpenForm
method; in the popup form you can then set the DefaultValue property of the
control bound to the linking field to the value passed on OpenArgs. Doable but
more work!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top