We help IT Professionals succeed at work.

Load subforms on button click

Joy Gomez
Joy Gomez asked
on
Hello,

I have a Main form and 3 subforms.  Each of the 3 sub forms has more sub forms.  When I initially open the Main form, I do not want to load any of the 3 sub forms or their subforms.  I also have 3 buttons inside the Main form.  On the click of these buttons, I want to open the corresponding subforms and its subforms.

Thanks much in advance for your help
Joy
Comment
Watch Question

SILVER EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:

This is done by setting the RecordSource of the subform(s) when you click that button:


Dim f As Form
Set f = <the subform in question>
f.RecordSource = Name of query or an SQL sentence.

When done, the subform will update at once.

Owner, Dev-Soln LLC
SILVER EXPERT
Most Valuable Expert 2014
Top Expert 2010
Commented:

When I have subforms on a form, I generally do not load them until after the main form is loaded, usually in the Form_Current event.  And a lot of times, when I use the tab control to display subforms, I use a similar technique.  But instead of playing with the subforms recordsource, I use the subform controls SourceObject, and Visible properties.

Private Sub cmd_Sub1_Display_Click
    me.sub1.SourceObject = "subform1 name"
    me.sub1.visible = true
End Sub

Author

Commented:
Hi Dale,

Thanks for the note...

Are you saying that when I load the main form, I should by default make the subforms visibility=false... then on button click make the subform visibility=true? Does that have the same effect as loading a form?  The reason I am doing this, is because I have so many subforms, that it takes forever to load the main form, and I am trying to reduce this time.

Thanks
Joy
BRONZE EXPERT
Distinguished Expert 2017
Commented:

By definition,

the subform control should always be hidden with its visible property set to No

the subform's ControlSource should be blank


Then when the user clicks a button, the two properties are set.  You might want the buttons to toggle so if the subform is hidden, show it but if it is visible, hide it.

Dale FyeOwner, Dev-Soln LLC
SILVER EXPERT
Most Valuable Expert 2014
Top Expert 2010

Commented:

I agree with Pat, although I think she meant the subform controls  "SourceObject" should be blank, not "ControlSource"


Dale

Author

Commented:
Worked!  Thanks All!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.