简体   繁体   English

从Excel VBA运行访问查询,模块名称包含斜杠

[英]Run Access query from Excel VBA with module name containing slash

I want to run a MS Access query from Excel VBA. 我想从Excel VBA运行MS Access查询。 For that I'm using ADODB.Connection and ADODB.Command to call the query. 为此,我使用ADODB.Connection和ADODB.Command来调用查询。 In principle it works, but there is an issue with the name, because it contains slashes: "Query_3/6/1/1". 原则上它可以工作,但是名称有问题,因为它包含斜杠:“ Query_3 / 6/1/1”。 Running the script below, I get an error message like "The Microsoft Office Access database engine cannot find the input table or query 'Query_3'." 运行下面的脚本,我收到一条错误消息,例如“ Microsoft Office Access数据库引擎找不到输入表或查询'Query_3'。” So it doesn't consider the rest of the name following the slash. 因此,它不会考虑斜杠后的其余名称。 I already tried escaping it with brackets [], but it doesn't help and other than that I didn't find a solution. 我已经尝试用方括号[]进行转义,但是它没有帮助,除此之外,我没有找到解决方案。

Renaming the module works, but there are lots of them and there are already other dependendies, so that is not really a solution. 重命名模块是可行的,但是其中有很多,并且已经存在其他依赖项,因此这并不是真正的解决方案。

I'm very glad for any help! 我很高兴获得任何帮助!

Dim con As ADODB.Connection
Dim cmd As ADODB.Command

With con
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .Open "C:\Users\...\file.accdb"
End With

With cmd
    .ActiveConnection = con
    .CommandText = "Query_3/6/1/1"
    .CommandType = adCmdStoredProc
End With

Set rs = New ADODB.Recordset
rs.Open cmd
...

I don't think you are going to like the answer. 我认为您不会喜欢这个答案。 I did some testing and Excel is just not going to honor that naming convention. 我做了一些测试,Excel只是不遵守该命名约定。 I even called a query built from that Query_3/6/1/1 and it would not work. 我什至调用了从该Query_3 / 6/1/1构建的查询,该查询不起作用。 I encountered this same issue with queries that used Nz() function. 我在使用Nz()函数的查询中遇到了同样的问题。 Had to redesign queries. 必须重新设计查询。

Only alternative I can see is a procedure in Access that writes the query records to a 'temp table' (table is permanent, records are temporary) and the Excel calls this procedure then opens recordset based on the temp table. 我只能看到的另一种选择是Access中的一个过程,该过程将查询记录写入“临时表”(表是永久性的,记录是临时的),Excel会调用此过程,然后基于临时表打开记录集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM