Code to select which subform to open from mainform.

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 16:42
Joined
Apr 1, 2019
Messages
731
This maybe simple but i could not find an example. If i have a form 'main' and wish to open either of 2 different bound subforms, say 'subform1' or 'subform2' by button press. How would i do it? Or is this the wrong approach? Appreciate the heads up.
 
What do you mean by "subform" - is this a form sitting on another form or opening independently?

Set SourceObject property of subform container control or consider a Navigation Form structure.
 
see post #2.
for demo, create a new Form and add two command button.
also add a Subform/subreport control.
on the Click event of the each button, add codes:
Code:
Private Sub Button1_Click()
Me.Child0.SourceObject = "subform1" 'Child0 is the name of the subform/subreport on this demo
End Sub

Private Sub Button2_Click()
Me.Child0.SourceObject = "subform2"
End Sub
 
for the code part, if you're using something like Access, you can try this:
vbaCopy code
Private Sub Button_Click()
If condition1 Then
DoCmd.OpenForm "subform1"
ElseIf condition2 Then
DoCmd.OpenForm "subform2"
End If
End Sub
You'd have to replace condition1 and condition2 with your actual conditions to decide which subform to open when the button's clicked. Hope this helps!
 
Why do you need an example?

Simple IF test and use June's method would be my preferred method with one subform control.

If you have to have two, then still a simple IF test and reveal one of the controls.

The first method is neater IMHO.
 
@June7 , a subform on a mainform. Depending upon which option the user selects either of 2 subforms are to be opened on the mainform.
 
All. Thanks just needed some direction. Appreciate it, should be good to go now.
 

Users who are viewing this thread

Back
Top Bottom