MS Access:我的表格在vba中出现错误 [英] MS Access: An error in vba of my form

查看:74
本文介绍了MS Access:我的表格在vba中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple MS Access form that has 3 objects: a text box, a list box, and a button. The intended use of the form is as follows: the user enters a name in the text box, selects an item from the list box, then clicks the button to add the data to a table.

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

However, when I click on the button, I keep getting the error message, "You can't reference property or a method for a control unless the control has the focus."

下面是我的代码.谢谢!

Below is my code. Thanks!

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

推荐答案

仅当TextBox具有焦点时,才能使用.Text属性.尝试改用.Value属性.

The .Text property can only be used when the TextBox has focus. Try using the .Value property instead.

尝试替换下面的行

CustomerName = Customer_TextBox.Text

CustomerName = Customer_TextBox.Value

这篇关于MS Access:我的表格在vba中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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