简体   繁体   中英

Can I use VBA in MS Access to add a control to a tab control (TabCtl) object?

I want to use VBA in MS Access to dynamically add a subform control to a tab control object on a form. Basically, I have a 99% working solution where users need to generate a custom form. They can select the type and number of drop-downs they want, and it will generate a main form and paste in a subform with all of those controls. If there are too many controls (22" form limit in Access), it splits them into multiple subforms -- hence the tab control. The only thing I can't get it to do is paste the new subforms into the tab control itself.

Here's a simplified version of what I'm trying to do. In my research I did find VBA methods to add controls to TabStrips and TabPages, but those aren't relevant to MS Access. The TabCtl object itself doesn't seem to have any properties related to adding controls within it (yet this is something you can obviously do in the GUI).

Sub AddControlToTabControl()

    Dim frm As Form
    Dim subForm As Form
    Dim tabCtl As tabControl
    
    Set frm = Forms!Form1
    Set subForm = Forms!Form1_subForm
    Set tabCtl = frm!tabCtl
    
    DoCmd.OpenForm frm.Name
    
    'Here are some things that aren't valid
    tabCtl.Pages(0).Add (subForm)        'Pages collection does not have an Add property
    tabCtl.Controls.Add (subForm)        'Controls collection does not have an Add property
    tabCtl.Add (subForm)                 'tabCtl object does not have an Add property

End Sub

I reviewed the Tab Control object Microsoft documentation , including all of the properties I thought would make sense, but there doesn't seem to be anything on how to put a control specifically on the tab control itself.

I also used VBA to paste the subform onto the main form using coordinates that would be on top of the tab control, but it just created overlapping objects.

You're correct that the TabCtl object in MS Access does not have any properties related to adding controls within it. However, you can use the "Pages" collection of the TabCtl object to add new TabPages, and then add controls to those TabPages.

Here's an example of how you can add a new TabPage to the TabCtl object, and then add a subform to that TabPage:

Sub AddControlToTabControl()

Dim frm As Form
Dim subForm As Form
Dim tabCtl As tabControl
Dim newTabPage As TabPage

Set frm = Forms!Form1
Set subForm = Forms!Form1_subForm
Set tabCtl = frm!tabCtl

Set newTabPage = tabCtl.Pages.Add(, , "New Tab")
newTabPage.Controls.Add(subForm.Name)
subForm.Visible = True

End Sub

You can also use the.Pages.Add method to add a new TabPage and set a name for it.

In this example, the new TabPage is created and added to the TabCtl object, the subform is added to the new TabPage and the subform is set to visible.

You can also use the.Pages.Add method to add new TabPages and set a name for it.

You can also use the.Pages.Add method to add new TabPages and set a name for it and then add the subform to the new TabPage, Like this:

Sub AddControlToTabControl()
Dim frm As Form
Dim subForm As Form
Dim tabCtl As tabControl
Dim newTabPage As TabPage

Set frm = Forms!Form1
Set subForm = Forms!Form1_subForm
Set tabCtl = frm!tabCtl

Set newTabPage = tabCtl.Pages.Add("New Tab", "New Tab")
newTabPage.Controls.Add(subForm.Name)
subForm.Visible = True

End Sub

It's worth noting that you can only add a control to a TabPage once. If you try to add the same control again, it will give you an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM