通过Access VBA代码保存查询

时间:2011-03-13 00:34:15

标签: vba ms-access access-vba

我需要通过VBA代码“物理地”创建SQL查询。 我知道如何在VBA中执行查询,但我需要将其保存在菜单中。如果可以,我会张贴一张照片。我会试着让你想象。

在MS Access的主屏幕中,左侧有一个栏。

为了说清楚,“Consulta”是葡萄牙语查询。

如果你不理解我,请原谅我缺乏解释。我很乐意再次解释。

1 个答案:

答案 0 :(得分:7)

我想你想要:

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

编辑重新评论

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

请注意,删除不存在的查询会导致错误。

 DoCmd.DeleteObject acQuery, "NewQuery"
相关问题