VBA中的访问选择组合框列返回”函数未定义”错误

Access selecting combobox column in VBA returns “function not defined”-error

当尝试在VBA中访问我的组合框的特定列值时,我将收到运行时错误消息,告诉我"未定义函数"。

我的调用结构是否有错误或其他错误?

我之前完全清除了Topics表,现在想从组合框中插入两个同伴的新数据:

1
2
 strAdd ="INSERT INTO TopicsTbl ( TopicName, CategoryID, CategoryName)" & _
"VALUES (Forms!frmAdd_Rename_Delete_Topic!txtTopicName, Forms!frmAdd_Rename_Delete_Topic!txtCategoryName.column(0), Forms!frmAdd_Rename_Delete_Topic!txtCategoryName.column(1));"

值调用在vba中位于一行中,因此请假定" line1"


问题出在您引用列号的方式上。您可以使用参数或字符串连接来解决此问题:

使用字符串连接:

1
2
strAdd ="INSERT INTO TopicsTbl ( TopicName, CategoryID, CategoryName)" & _
"VALUES (Forms!frmAdd_Rename_Delete_Topic!txtTopicName," & Forms!frmAdd_Rename_Delete_Topic!txtCategoryName.column(0) &",""" & Forms!frmAdd_Rename_Delete_Topic!txtCategoryName.column(1) &""");"

我假设数据库中的第一列是数字,第二列是字符串。字符串串联是一种不好的做法,因为它将数据库开放给SQL注入,并在存储包含字符串定界符的值时导致错误。

或者,使用参数(我使用的是隐式参数类型,有时需要显式类型):

1
2
3
4
5
6
7
strAdd ="INSERT INTO TopicsTbl ( TopicName, CategoryID, CategoryName)" & _
"VALUES (Forms!frmAdd_Rename_Delete_Topic!txtTopicName, ?, ?);"
Dim qd As DAO.QueryDef
Set qd = CurrentDb.CreateQueryDef("",strAdd)
qd.Parameters(0) = Forms!frmAdd_Rename_Delete_Topic!txtCategoryName.column(0)
qd.Parameters(1) = Forms!frmAdd_Rename_Delete_Topic!txtCategoryName.column(1)
qd.Execute