change subform programatically

  • Thread starter Thread starter Tara
  • Start date Start date
T

Tara

I have a form that currently has 4 subforms. It makes the form way too
cluttered. What I would like to do is create one subform and have the data,
controls, etc change through code based on which command button is clicked.
I know this can be done, but i have no idea where or how to start!

Any help is appreciated.
 
Build a new form called "Form Select" or something like that. Make "Form
Select your subform on the mainform. Put 4 command buttons on the Form Select
form. Make the command buttons open the four forms. Use the wizard to do that
for you. The command button wizard will build the code for you.
 
Where sctlSubformContainer is the name of the subform *control* on your
form, not the name of the subform itself. (If you drag & drop a subform onto
a form, the wizard will probably give the containing control the same name
as the subform. This can be very confusing. Change the name of the
container.)


Sub cmdShowNamesInSubform_Click()
Me.sctlSubformContainer.SourceObject = "frmNameSubform"
End Sub

Sub cmdShowInvoicesInSubform_Click()
Me.sctlSubformContainer.SourceObject = "frmInvoiceSubform"
Me.sctlSubFormContainer.Form.Filter = "[InvoiceNo] = " & me.txtInvoiceNo
Me.sctlSubFormContainer.Form.FilterOn = True
End Sub
 
I'm trying that now actually, and it's not working. The parent form opens
with the primary subform already visible. For some reason, when I click on
another tab to open a different subform, the top portion of my main form
disappears. I'm not sure why, since all the subforms are sized the same,
etc. I'm going to investigate a bit more and see if I can figure out what's
going on. If I can't get it fixed, I'll try George's idea.
 
Thanks George! I'll try your approach and see what happens.

George Nicholson said:
Where sctlSubformContainer is the name of the subform *control* on your
form, not the name of the subform itself. (If you drag & drop a subform onto
a form, the wizard will probably give the containing control the same name
as the subform. This can be very confusing. Change the name of the
container.)


Sub cmdShowNamesInSubform_Click()
Me.sctlSubformContainer.SourceObject = "frmNameSubform"
End Sub

Sub cmdShowInvoicesInSubform_Click()
Me.sctlSubformContainer.SourceObject = "frmInvoiceSubform"
Me.sctlSubFormContainer.Form.Filter = "[InvoiceNo] = " & me.txtInvoiceNo
Me.sctlSubFormContainer.Form.FilterOn = True
End Sub

--
HTH,
George


Tara said:
I have a form that currently has 4 subforms. It makes the form way too
cluttered. What I would like to do is create one subform and have the
data,
controls, etc change through code based on which command button is
clicked.
I know this can be done, but i have no idea where or how to start!

Any help is appreciated.
 
Even if you use tabs, you might see a performance gain at startup if you
don't set the subform control SourceObject until the related tab becomes
active for the first time (rather than loading all subform automatically
when the main form loads).

A lot depends on how big/complicated the subforms and their recordsources
are.
 
Back
Top