通过 Access VBA 代码保存查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5286620/
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
Saving a Query via Access VBA code
提问by Patrick Villela
I need to "physically" create an SQL query via VBA code. I know how to execute a query in VBA, but I need to save it in the menu. If I could I would post a picture. I'll try to make you imagine.
我需要通过 VBA 代码“物理地”创建一个 SQL 查询。我知道如何在 VBA 中执行查询,但我需要将其保存在菜单中。如果可以我会发一张照片。我会尽量让你想象。
In the main screen of MS Access there's a bar on the left.
在 MS Access 的主屏幕中,左侧有一个栏。
Just to make it clear, "Consulta" is Query in Portuguese.
为了说明这一点,“Consulta”是葡萄牙语中的查询。
If you didn't understand me, please excuse my lack of explanation. I'll be pleased to explain again.
如果你不明白我的意思,请原谅我的解释不够。我很乐意再次解释。
回答by Fionnuala
I think you want:
我想你想要:
If Not IsNull(DLookup("Type", "MSYSObjects", "Name='MyNewQuery'")) Then
MsgBox "An object already exists with this name"
Else
CurrentDb.CreateQueryDef "MyNewQuery", "SELECT * FROM Table1"
End If
EDIT re comments
编辑重新评论
Sub UpdateQuery(QueryName, SQL)
''Using a query name and sql string, if the query does not exist, ...
If IsNull(DLookup("Name", "MsysObjects", "Name='" & QueryName & "'")) Then
''create it, ...
CurrentDb.CreateQueryDef QueryName, SQL
Else
''Other wise, update the sql.
CurrentDb.QueryDefs(QueryName).SQL = SQL
End If
End Sub
Note that deleting a query that does not exists will cause an error.
请注意,删除不存在的查询会导致错误。
DoCmd.DeleteObject acQuery, "NewQuery"