gpt4 book ai didi

ms-access - 微软 Access : An error in vba of my form

转载 作者:行者123 更新时间:2023-12-04 17:39:28 24 4
gpt4 key购买 nike

我有一个简单的 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

最佳答案

.Text属性只能在 TextBox 具有焦点时使用。尝试使用 .Value属性(property)代替。

尝试替换以下行

CustomerName = Customer_TextBox.Text


CustomerName = Customer_TextBox.Value

关于ms-access - 微软 Access : An error in vba of my form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7623686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com