问题
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