Code to select which subform to open from mainform. (1 Viewer)

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 23:41
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.
 

June7

AWF VIP
Local time
Today, 02:41
Joined
Mar 9, 2014
Messages
5,472
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.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:41
Joined
May 7, 2009
Messages
19,243
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
 

Oviziest

New member
Local time
Today, 03:41
Joined
Nov 13, 2023
Messages
5
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!
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:41
Joined
Sep 21, 2011
Messages
14,301
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.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 23:41
Joined
Apr 1, 2019
Messages
731
@June7 , a subform on a mainform. Depending upon which option the user selects either of 2 subforms are to be opened on the mainform.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 23:41
Joined
Apr 1, 2019
Messages
731
All. Thanks just needed some direction. Appreciate it, should be good to go now.
 

Users who are viewing this thread

Top Bottom