How to create controls at run time Access VB?

后端 未结 4 1894
终归单人心
终归单人心 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 02:10

    I took that upove code and simplified it as it was long winded, and turned it into a a sample code for my own future use. Hope it helps someone in the future.

    Public Sub runDynamicCreateControls()
        Dim FormName As String
        Dim NumControls As Integer
        Dim n As Long
        Dim ctl As Access.Control
        Dim ctlname As String
        On Error GoTo EH
        Application.Echo False
        FormName = "createcontrolF" 'Input Name of Form
        NumControls = 20 'input number of controls
        ctlname = "txt" 'input control name
        DoCmd.OpenForm FormName, acDesign
        For n = 1 To NumControls
            With Application.CreateControl(FormName, acTextBox, acDetail, , , 1000,1000, 1100, 600)
                .Name = ctlname & "_" & n
                .Visible = True
                .Tag = n
            End With
        Next 'n
        DoCmd.Close acForm, FormName, acSaveYes
        DoCmd.OpenForm FormName, acNormal
        GoTo FINISH
    EH:
        With Err
            MsgBox "Error:" & vbTab & .Number & vbCrLf _
                & "Source" & vbTab & .Source & vbCrLf _
                & .Description
        End With
    FINISH:
        Application.Echo True
    End Sub
    

提交回复
热议问题