3

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

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.

1 Answer 1

5

You can set the sql of a query to the output sql and use the query name in 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 +

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

1
  • 3
    Thank you this worked great, for other users that look at this if you require to delete the the query in the end like i did just add DoCmd.DeleteObject acQuery, "Query1"
    – KSM
    Sep 18, 2012 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.