SQL MS Access - 在 VBA 中按名称执行保存的查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9929009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MS Access - execute a saved query by name in VBA
提问by tdjfdjdj
How do I execute a saved query in MS Access 2007 in VBA?
如何在 VBA 中的 MS Access 2007 中执行保存的查询?
I do not want to copy and paste the SQL into VBA. I rather just execute the name of the query.
我不想将 SQL 复制并粘贴到 VBA 中。我宁愿只执行查询的名称。
This doesn't work ... VBA can't find the query.
这不起作用...... VBA 找不到查询。
CurrentDb.Execute queryname
回答by Taryn
You can do it the following way:
您可以通过以下方式执行此操作:
DoCmd.OpenQuery "yourQueryName", acViewNormal, acEdit
OR
或者
CurrentDb.OpenRecordset("yourQueryName")
回答by HansUp
You should investigate why VBA can't find queryname.
您应该调查为什么 VBA 找不到queryname。
I have a saved query named qryAddLoginfoRow. It inserts a row with the current time into my loginfotable. That query runs successfully when called by name by CurrentDb.Execute
.
我有一个名为qryAddLoginfoRow 的已保存查询。它将包含当前时间的一行插入到我的loginfo表中。该查询在按 name by 调用时成功运行CurrentDb.Execute
。
CurrentDb.Execute "qryAddLoginfoRow"
My guess is that either querynameis a variable holding the name of a query which doesn't exist in the current database's QueryDefs collection, or querynameis the literal name of an existing query but you didn't enclose it in quotes.
我的猜测是,queryname是一个变量,其中包含当前数据库的 QueryDefs 集合中不存在的查询名称,或者queryname是现有查询的文字名称,但您没有将其括在引号中。
Edit:
You need to find a way to accept that querynamedoes not exist in the current db's QueryDefs collection. Add these 2 lines to your VBA code just before the CurrentDb.Execute
line.
编辑:你需要找到一种方法来接受queryname当前数据库的QueryDefs集合中不存在。将这两行添加到该CurrentDb.Execute
行之前的 VBA 代码中。
Debug.Print "queryname = '" & queryname & "'"
Debug.Print CurrentDb.QueryDefs(queryname).Name
The second of those 2 lines will trigger run-time error 3265, "Item not found in this collection." Then go to the Immediate window to verify the name of the query you're asking CurrentDb
to Execute
.
那些2线的第二个将触发运行时错误3265,“项目不是这个集合中找到。”然后去立即窗口来验证你要求查询的名称CurrentDb
来Execute
。
回答by tahwos
To use CurrentDb.Execute, your query must be an action query, AND in quotes.
要使用 CurrentDb.Execute,您的查询必须是操作查询,并且在引号中。
CurrentDb.Execute "queryname"