DoCmd.OpenForm opens empty subform

  • Thread starter Thread starter jaworski_m
  • Start date Start date
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.
 
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!
 
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!
 
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.
 
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!
 
Back
Top