DoCmd.OutputTo + 使用 SQL 命令 + VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12478102/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:45:40  来源:igfitidea点击:

DoCmd.OutputTo + With a SQL Command + VBA

sqlvbams-access

提问by KSM

Hey everyone i did some research online but i couldn't get a solution, hopefully someone here can help. My go is to place a sql statement to a DoCmd.OutputTo

大家好,我在网上做了一些研究,但我找不到解决方案,希望这里有人可以提供帮助。我的目标是将 sql 语句放置到 DoCmd.OutputTo

for example

例如

      Set tempSql= CurrentDb.OpenRecordset("SELECT * FROM SpecialEvent WHERE [HG_ID] = " & HG_ID & ";")

      DoCmd.OutputTo acOutputQuery, "tempSql", acFormatXLS, strDir + "try.xls", 0

If anyone can guide me to building this approach, it would be much appreciated.

如果有人能指导我构建这种方法,我将不胜感激。

回答by Fionnuala

You can set the sql of a query to the output sql and use the query name in DoCmd.OutputTo

您可以将查询的 sql 设置为输出 sql 并使用 DoCmd.OutputTo 中的查询名称

tempSql= "SELECT * FROM SpecialEvent WHERE [HG_ID] = " & HG_ID

If IsNull(DLookup("name", "msysobjects", "name='query1'")) Then
    CurrentDb.CreateQueryDef "Query1", tempSql 
Else
    CurrentDB.QueryDefs("Query1").SQL = TempSQL
End If


DoCmd.OutputTo acOutputQuery, "Query1", acFormatXLS, strDir & "try.xls", 0

Note that the concatenator is & in VBA, not +

请注意,连接器在 VBA 中是 &,而不是 +

The plus sign can be used to concatenate, but it needs great care because of the way it works with nulls.

加号可用于连接,但由于它处理空值的方式,它需要非常小心。