简体   繁体   English

我可以在 MS Access 中使用 VBA 将控件添加到选项卡控件 (TabCtl) object 吗?

[英]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.我想在 MS Access 中使用 VBA 将子窗体控件动态添加到窗体上的选项卡控件 object。 Basically, I have a 99% working solution where users need to generate a custom form.基本上,我有一个 99% 的工作解决方案,用户需要在其中生成自定义表单。 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.他们可以 select 他们想要的下拉菜单的类型和数量,它会生成一个主窗体并粘贴到一个包含所有这些控件的子窗体中。 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.如果控件太多(Access 中的 22" 窗体限制),它会将它们拆分为多个子窗体——因此是选项卡控件。我唯一做不到的就是将新的子窗体粘贴到选项卡控件本身。

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.在我的研究中,我确实找到了 VBA 种向 TabStrip 和 TabPage 添加控件的方法,但这些方法与 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). TabCtl object 本身似乎没有任何与在其中添加控件相关的属性(但这显然可以在 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.我查看了Tab Control object Microsoft 文档,包括我认为有意义的所有属性,但似乎没有关于如何专门将控件放在选项卡控件本身上的任何内容。

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.我还使用 VBA 使用位于选项卡控件顶部的坐标将子窗体粘贴到主窗体上,但它只是创建了重叠对象。

You're correct that the TabCtl object in MS Access does not have any properties related to adding controls within it.您是正确的,MS Access 中的 TabCtl object 没有任何与在其中添加控件相关的属性。 However, you can use the "Pages" collection of the TabCtl object to add new TabPages, and then add controls to those TabPages.但是,您可以使用 TabCtl object 的“Pages”集合来添加新的 TabPage,然后向这些 TabPage 添加控件。

Here's an example of how you can add a new TabPage to the TabCtl object, and then add a subform to that TabPage:下面是一个示例,说明如何向 TabCtl object 添加新的 TabPage,然后向该 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.您还可以使用.Pages.Add 方法添加一个新的TabPage 并为其设置名称。

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.在这个例子中,新的TabPage被创建并添加到TabCtl object,子表单被添加到新的TabPage并且子表单被设置为可见。

You can also use the.Pages.Add method to add new TabPages and set a name for it.您还可以使用 .Pages.Add 方法添加新的 TabPages 并为其设置名称。

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:你也可以使用.Pages.Add方法添加新的TabPages并为其设置名称,然后将子表单添加到新的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", "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.值得注意的是,您只能将控件添加到 TabPage 一次。 If you try to add the same control again, it will give you an error.如果您尝试再次添加相同的控件,则会报错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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