Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open child form with button?

Status
Not open for further replies.

kc112

Technical User
May 16, 2011
41
US
Please help!!

I have a main form and a subform with a relationship link. I used a button with the openform command and built the following code:

Private Sub HPIComplaint1_Click()
On Error GoTo Err_HPIComplaint1_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "HistoryofPresentIllness"

stLinkCriteria = "[Key]=" & Me![Key]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_HPIComplaint1_Click:
Exit Sub

Err_HPIComplaint1_Click:
MsgBox Err.Description
Resume Exit_HPIComplaint1_Click

End Sub

This correctly opens the form, but it opens as a parallel form NOT a child linked form. I need this to be a child linked form OR have the PK and FK "KEY" fields to autopopulate and link.

Where did I go wrong??

Please and thank you!! Kristyn
 
Where did I go wrong
By using the OpenForm method.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Okay...so if I do not use the openform.command, then what should I use???

If I use the toolbar to add a subform, it correctly populates the foreign key, BUT it also takes up screen space and shows the entire form. I want to use a button to open the child form.

What do I do?
 
I thought this was familiar when I saw the subject...

thread702-1649781
 
Correct again, Lameid!

When I realized that I was opening a parallel form NOT a subform. I knew my question was stated wrong. What I need to do is open the subform/child form via a button and have the PK FK link.

Again, I tried the above code and I also tried the code you posted in my other thread...both of which to no avail. It would not populate the FK field.

In the end, I added key!treatmentplandata!key to the default value in the properties tab of the FK field on the parallel form opened via the button. I am hoping that this still correctly links to two since I have them linked via a relationship.

Your thoughts?
 
How are ya New kc112 . . .

A subform is [blue]a form embedded on another form[/blue]. The subforms [blue]Master/Child link[/blue] properties are used to synchronize the two forms.

A form opened using DoCmd.OpenForm is an [blue]independent form[/blue] and is typically synchronized with another form thru criteria of a query/SQL used as the recordsource of the subform. The [blue]On Current[/blue] event of the parent form is then used to requery the independent form.

You keep saying subform, when it looks like you mean an independent form ... So which is it?

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
For clarity, subreports and subforms are setup the same way. So if you want to 'link' forms without the hassle of additional work, you should use a subform.

If on the otherhand you want to 'link' another form (parallel is not Access termonology as Aceman points out), you have to do a number of things.... In the other thread where you said you had stated your case wrong and set the default value, I suggested you needed to use code similar to the start of this thread as an additional step.

The only thing this does not potentially do is change the second form selected / filtered records when the first form's record changes (different key).

In that case you would need to change the filter on the second form on the first form's On Current Event as a third step, again as Aceman pointed out.
 
I sometimes cheat when I do this set up. My popup form is actually a main form sub form setup. The main form has only one or a couple of fields. It is bound to the similar table or query of the original form. I display just a enough to show the parent record. This/these field/s is locked and uneditable. Normally there are no navigation buttons. The subform on this form is basically the same subform you would have had if you had put the subform on the original form.

Now all I have to do from the original mainform is:
dim PK as somedatatype
PK = somePKfield
docmd.openform "myPopUpFormName",,,,"someField =" & me.SomeOtherfield
me.requery
me.recordset.findfirst "someField = " & PK

So the pop up would have some basic information at the top, and all child records are displayed. If no child records exist, then the new records would automatically get the new foreign key.

This simplifies some of the code needed for both synchronization and setting default values for new records.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top