How to create controls at run time Access VB?

后端 未结 4 1884
终归单人心
终归单人心 2020-12-15 01:16

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

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 01:53

    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:

    • Limited to 754 controls over the lifetime of the form (this is not reset by deleting them, so you are likely to run into this limit quickly)
    • Must be done in Design view (so it can't be done in mde/accde)
    • It is questionable how it will perform in a multi-user environment.

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

    Duplicate Question: How do you dynamically create controls on a MS Access form?

    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).

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

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

    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
    

提交回复
热议问题