我正在尝试从excel vba运行存储在访问数据库中的查询。如果我在Access数据库中打开并运行它,但在从VBA模块运行它时导致错误,则查询运行正常。 该查询使用名为“Getlist”的存储函数,该函数位于访问数据库模块中。以下是查询。
SELECT paramconcat.parameter, GetList("Select concat From paramconcat As T1 Where T1.parameter = """ & [paramconcat].[parameter] & """","",", ") AS Concatenated INTO results
FROM paramconcat
GROUP BY paramconcat.parameter;
VBA模块
Sub test1()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
With con
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open "C:\Users\erukhq\Documents\My Received Files\paramcheck.accdb"
End With
con.Execute "ParamconcatQ"
con.Close
End Sub
我得到的错误如下。
答案 0 :(得分:1)
您只能在从Access外部调用的查询中使用内置的本机VBA函数,因此在Access中运行查询时,您的自定义函数将起作用,但在通过ADO调用时它将不起作用。
您还需要一个ADODB.Command对象。
'...
Dim cmd as ADODB.Command
Set cmd = New ADODB.Command
Cmd.CommandText = "yourqueryname"
Cmd.CommandType = acCommandUnknown 'you might need to try different values of the CommandType enum.
Cmd.ActiveConnection = conn
Set rst = new ADODB.record set
Rst.open cmd