【问题标题】:MS Access: An error in vba of my formMS Access:我的表单中的 vba 错误
【发布时间】:2011-10-01 23:44:17
【问题描述】:

我有一个简单的 MS Access 表单,它有 3 个对象:一个文本框、一个列表框和一个按钮。表单的预期用途如下:用户在文本框中输入名称,从列表框中选择一个项目,然后单击按钮将数据添加到表中。

但是,当我单击按钮时,我不断收到错误消息,“除非控件具有焦点,否则您无法引用控件的属性或方法。”

下面是我的代码。谢谢!

Private Sub addRecord_button_Click()
    Dim CustomerName As String
    Dim CustomerType As String

    On Error GoTo Errhandler

    CustomerName = "[name not selected]"
    CustomerType = "[type not selected]"

    CustomerName = Customer_TextBox.Text

    Select Case Type_ListBox.ListIndex
        Case 0
            CustomerType = "Type 1"
        Case 1
            CustomerType = "Type 2"
        Case 2
            CustomerType = "Type 3"
    End Select

    'MsgBox ("Name: " & CustomerName & " and Type: " & CustomerType)

    DoCmd.RunSQL "INSERT INTO Customer VALUES (CustomerName, CustomerType);"

Errhandler:
    MsgBox ("The following error has occured: " & Err & " - " & Error(Err))
End Sub

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    .Text 属性只能在 TextBox 具有焦点时使用。请尝试改用 .Value 属性。

    尝试替换下面的行

    CustomerName = Customer_TextBox.Text
    

    CustomerName = Customer_TextBox.Value
    

    【讨论】:

    • 或者,完全省略一个属性,即Me!Customer_Textbox(也应该指定父表单,即Me!,因为这将它与对变量的引用区分开来)。跨度>
    猜你喜欢
    • 2021-08-13
    • 2012-06-09
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多