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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attach Sub-Forms in VBA Code

Status
Not open for further replies.

vbc22

Technical User
Dec 4, 2004
70
CA
Hi there,

Just wondering can one append sub-forms programmatically through VBA code?

The reason I ask is because when working in design view, I don't want to clutter up the main form with sub forms on it. I figure, I can work in a more organized fashion by having it decoupled.

I'd create the controls and write the code separately on each form, and then when done just add some code back in the main form to insert them. Code would include positioning the sub-forms as such.

You can create tables through code, so I am wondering if it is possible to also append standalone forms as sub-forms onto other forms through code.

Regards.
 
Hmm...turns out that after looking more thoroughly at the help files, there's a CreateControl method, which you can use.

I got my code to work, but the unfortunate fact is, as stated in the help file, it can only be used in form design view. So with that, it answers my question.

No, you cannot create a subform dynamically with code...at least during run-time.
 
I decided to not close this topic entirely...

If I should be corrected, please by all means, let me know. :)
 
Hi
Design view only is what I have, too. Have you considered using a hidden subform, which could be resized and have the contents changed as suits?
Me.MySubForm.SourceObject = "NameofForm
 
Hey Remou,

Yeah, that was my original plan...and something that I've done before, actually.

In another application, I had set about 9 subforms onto the main form; each shrunk down to cube sizes and set to invisible. Then, with events triggered, a particular is set to visible, resized, and positioned.

So I knew the workaround, but just didn't want to give-up on the idea of adding it dynamically. The advantage here could be that you save yourself load-up time because all those subforms would have to be loaded upon startup.

Regards.
 
I got a better solution!

This was actually suggested by another online user. The idea is to use an unbound subform control and reset it's object source to the form you want.

So what I did as a test was create that unbound form, and depending on which toggle button selected, it would switch the subform based on changing its object source.

Code:
Private Sub frmFormToggles_AfterUpdate()
    Select Case Me.frmFormToggles
        Case 1
            Me.subF.SourceObject = "f_form1"
        Case 2
            Me.subF.SourceObject = "f_form2"
        Case 3
            Me.subF.SourceObject = "f_form3"
    End Select

End Sub

Oh yeah! That's a cool design approach. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top