如何在运行时访问VB创建控件? [英] How to create controls at run time Access VB?

查看:68
本文介绍了如何在运行时访问VB创建控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时使用Microsoft Access中的VB代码创建控件?经过一番挖掘,我发现使用CreateControl函数是可行的.问题是我在网上找到的每个随机论坛都有一些类似于以下的代码:

How can you create controls at run time with VB code in Microsoft Access? after some digging I found that this is possible with the CreateControl function. The problem is every random forum I find online has some code similar to this:

Private Sub Button_Click()
    Call AddLabel
End Sub

Function AddLabel()
    Set ctl = CreateControl("MyForm", acLabel, acDetail, "", "", 0, 0, 100, 100)
    With ctl
       .name = "myTextBox"
       .Caption = "Hello World!"
       .Height = 50
       .Width = 100
       .FontSize = 11
       .Visible = True
    End With
End Function

但是此代码似乎无法创建可见标签.

but this code appears to not create a visible label.

就我而言,我只是在尝试学习如何使其正常工作.因此,我创建了一个带有按钮的空白表单,单击该按钮将创建一个标签,上面写着"Hello world!".我希望发生的是,单击按钮时,文本框将显示在表单的左上方.我很好奇是否有人可以帮助我向我展示一些实际有效的代码的简单示例.

In my case I'm just trying to learn how to get this to work. So I created a blank form with a button that when clicked will create a label that says "Hello world!". What I'm expecting to happen is a textbox will display in the top left of the form when the button is clicked. I'm curious if anyone could help show me a simple example of some code that actually works.

在有人说我可以创建标签并将其隐藏然后更改其可见性之前,我知道.但是我想知道如何动态创建控件,使这个简单的示例真正起作用将极大地帮助我理解.

Before anyone says I can create a label and hide it then change its visibilty property, I know. But I'd like to know how to create controls dynamically and getting this simple example to actually work will greatly help my understanding.

推荐答案

您需要的文档在此处(这些文档专门用于Access VBA):

The documentation you need is here (these are specifically for Access VBA):

  • Application.CreateControl Method (Office 2007)
  • Application.CreateControl Method (Office 2003)

根据文档,此功能有一些大限制:

According to the documentatin, there are some big limitations to this feature:

  • 在表单的生命周期内仅限于754个控件(不会通过删除它们进行重置,因此您很可能会很快遇到此限制)
  • 必须在设计"视图中完成(因此不能在mde/accde中完成)
  • 在多用户环境中如何运行会产生疑问.

由于这些限制,因此不建议这样做,除非您最初是用来设计表单的.

Because of these limitations, it is inadvisable, unless you are using to design forms initially.

重复的问题:您如何动态在MS Access表单上创建控件?

为响应OP的建议,这是我的测试代码,该代码能够添​​加40个控件并重复该过程50次,而不会超出754个限制(我在测试中重用了40个名称).

In response to the OP's suggestion, here is my test code which was able to add 40 controls and repeat the process 50 times without exceeding the 754 limit (I reused 40 names in my test).

注释1 这是不可取的,因为它只能在设计视图中完成,而该视图在mde/accde中不起作用.

Caveat 1 This is inadvisable because it can only be done in design view which will not work in an mde/accde.

注意事项2 :在多用户环境中如何运行会产生疑问.

Caveat 2: It is questionable how it will perform in a multi-user environment.

此代码来自带有两个按钮的表单.它将打开第二个名为"Form2"的表单

This code is from a form with two buttons. It opens a second form named "Form2"

Option Compare Database
Option Explicit

Private Const FORM_NAME As String = "Form2"
Private m_nCounter As Long

Private Sub cmdCreate_Click()
    runDynamicForm
End Sub

Private Sub cmdRepeat_Click()

    Dim n As Long

    m_nCounter = 0

    For n = 0 To 50
        runDynamicForm
        DoEvents
        DoCmd.Close acForm, FORM_NAME, acSaveNo
        DoEvents
    Next 'n

    MsgBox m_nCounter

End Sub

Private Sub runDynamicForm()

    Const DYNAMIC_TAG As String = "dynamic"

    Dim n As Long
    Dim frm As Form
    Dim ctl As Access.Control

    On Error GoTo EH

    Application.Echo False

    DoCmd.OpenForm FORM_NAME, acDesign
    Set frm = Application.Forms(FORM_NAME)

    For n = frm.Controls.Count - 1 To 0 Step -1
        Set ctl = frm.Controls(n)
        If ctl.Tag = DYNAMIC_TAG Then
            Application.DeleteControl FORM_NAME, ctl.Name
        End If
    Next 'n

    For n = 1 To 20

        With Application.CreateControl(FORM_NAME, acLabel, acDetail, , , 400, n * 300, 1500, 300)

            .Name = "lbl" & n
            .Caption = "Question " & n
            .Visible = True
            .Tag = DYNAMIC_TAG

        End With

        With Application.CreateControl(FORM_NAME, acTextBox, acDetail, , , 2000, n * 300, 3000, 300)

            .Name = "txt" & n
            .Visible = True
            .TabIndex = n - 1
            .Tag = DYNAMIC_TAG

        End With

        m_nCounter = m_nCounter + 2

    Next 'n

    DoCmd.Close acForm, FORM_NAME, acSaveYes

    DoCmd.OpenForm FORM_NAME, acNormal

    GoTo FINISH

EH:
    With Err
        MsgBox "Error:" & vbTab & .Number & vbCrLf _
            & "Source" & vbTab & .Source & vbCrLf _
            & .Description
    End With

FINISH:

    Application.Echo True

End Sub

这篇关于如何在运行时访问VB创建控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆