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

ⅰ亾dé卋堺 提交于 2019-12-11 15:10:56

问题


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?

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));"

the value call is in one line in vba, so please assume the "line1" & _ "line2" is not the problem. (it is poorly shown here)

Thanks in advance!


回答1:


The problem lies in the way you're referring to the column number. You can solve this by using parameters or string concatenation:

Using 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) & """);"

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


来源:https://stackoverflow.com/questions/48867477/access-selecting-combobox-column-in-vba-returns-function-not-defined-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!