在当前表单上创建控件

时间:2020-08-03 20:21:10

标签: vba ms-access

我正在尝试使用一个模板表格来加载表格中的设置。例如,我将有一个具有不同表单ID的主表,然后是一个文本框表,该文本框表将具有文本框的列表,这些文本框也具有它们所属的表单ID,它们的位置,它们的值等。问题是当我在其中打开表单时设计模式,代码无法运行,如果我尝试将视图设置为设计模式,则会收到错误消息,提示“此时您不能切换到其他视图”,代码为2174。这是我正在使用的代码现在,所有值都是硬编码的,所以我知道我从数据库中读取的内容没有任何错误。任何帮助将不胜感激!

Option Compare Database

Private Sub Form_Load()
    
    loadSettings
    
End Sub

Private Sub loadSettings()
    Dim ctl As Access.Control

    On Error GoTo ERR_Line
        
        DoCmd.RunCommand acCmdDesignView
        Set ctl = CreateControl(Me.Name, acTextBox, acDetail, , , 100, 100, 100, 100)
        ctl.Name = "txt1"
        ctl.Value = "test"
        DoCmd.RunCommand (acCmdFormView)

ERR_Line:
    MsgBox Err.Description & " Error Number " & Err

End Sub

如果我在其中使用此代码的模块,它将进入设计模式,但会给出“ Microsoft Access无法添加,重命名或删除您请求的控件”。错误号29054。

Option Compare Database

Sub loadSettings(frmName As String)
    Dim frm As Form
    Dim ctl As Access.Control

    On Error GoTo ERR_Line
        
        Set frm = Application.Forms(frmName)
        DoCmd.Close acForm, frmName
         
        DoCmd.OpenForm frmName, acDesign
        
        Set ctl = CreateControl(frmName, acTextBox, acDetail, , , 100, 100, 100, 100)
        ctl.Name = "txt1"
        ctl.Value = "test"
        DoCmd.Close acForm, frmName, acSaveYes
        DoCmd.OpenForm frmName, acNormal, , , , , "asdf"

ERR_Line:
    MsgBox Err.Description & " Error Number " & Err

End Sub

0 个答案:

没有答案
相关问题