VBA中的访问选择组合框列返回“函数未定义"错误 [英] Access selecting combobox column in VBA returns "function not defined"-error
问题描述
当尝试在VBA中访问我的组合框的特定列值时,我将收到运行时错误消息,告诉我未定义函数".
when trying to access a specific column value of my combobox in VBA, I will get a runtime error telling me the "function is not defined".
我的调用结构是否有错误或其他错误?
Is there any mistake in my calling structure or another error?
我之前完全清除了Topics表,现在想从组合框中插入两个同伴的新数据:
I cleared the Topics-table completely before and now want to insert the new data from the combobox with two coulmns:
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"& _"line2"不是问题. (此处显示不佳)
the value call is in one line in vba, so please assume the "line1" & _ "line2" is not the problem. (it is poorly shown here)
提前谢谢!
推荐答案
问题出在您引用列号的方式上.您可以使用参数或字符串串联来解决此问题:
The problem lies in the way you're referring to the column number. You can solve this by using parameters or string concatenation:
使用字符串串联:
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注入,并在存储包含字符串定界符的值时导致错误.
I'm assuming the first concatenated column is a number, and the second one is a string in your database. String concatenation is a bad practice, since it opens up your database to SQL injection, and causes errors when storing values that contain string delimiters.
或者,使用参数(我使用的是隐式参数类型,有时需要显式类型):
As an alternative, use parameters (I'm using implicit parameter types, explicit types are needed sometimes):
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
这篇关于VBA中的访问选择组合框列返回“函数未定义"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!