creating excel file from access with docmd

kw66722
kw66722 used Ask the Experts™
on
I am creating an excel spreadsheet from access where I build the query in the code behind using docmd.
Dim excelquery As String
Dim excelName As String
excelquery  =  "SELECT a,b, c  FROM tableSql where a = 123"
excelName = "Excel_" & Month(Date) & Day(Date) & Year(Date) & ".xls"

DoCmd.OutputTo acOutputQuery, excelquery, acFormatXLS, excelName, True

When I run it I get a message that the access database could not find the object "Select ..."

Is there another way to do this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
in the command line that you use, it needs a saved query..

if you create a query using the QBE with this SQL statement
SELECT a,b, c  FROM tableSql where a = 123

save the query as myQuery, then you can use in the command line


DoCmd.OutputTo acOutputQuery, "myquery", acFormatXLS, excelName, True

Author

Commented:
I am creating the query in the afterUpdate event of the form.  I am not sure how to save it as a query when I build it.
Top Expert 2016

Commented:
first create a query then save it.

is this query you are building will be the same but different criteria?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
They query changes based on multiple fields on the form.  There are many different combinations for the where statement.
Top Expert 2016
Commented:
you can do this
assuming you saved a query from above as myQuery

Dim excelquery As String
Dim excelName As String
dim qd as dao.querydef
excelquery  =  "SELECT a,b, c  FROM tableSql where a = 123"
excelName = "Excel_" & Month(Date) & Day(Date) & Year(Date) & ".xls"

set qd=currentdb.querydefs("myQuery")
qd.sql=excelquery


DoCmd.OutputTo acOutputQuery, "myquery", acFormatXLS, excelName, True
Commented:
DAO.Querydef was not defined in my version of access. I did the follow and it worked.  Do you think I should delete the table when done?

Dim curDatabase As Object
Dim qry As Object

Set qry = curDatabase.CreateQueryDef("ToExcel", excelquery)

DoCmd.OutputTo acOutputQuery, "ToExcel", acFormatXLS, excelName, True
Top Expert 2016

Commented:
<Do you think I should delete the table when done?>
Yes you can, it is up to you.
but, like what i posted above, you can reuse the query by changing its definition.
Top Expert 2016

Commented:
btw, what access version are you using?

to enable DAO.Querydef, you have to add to your References the Microsoft DAO 3.6 Object Library

Author

Commented:
I am using access 2013.  I thought about doing that but I wanted something that I could keep copying and not worrying about libraries not be available.
Top Expert 2016

Commented:
hmm, the DAO library should be available with version 2013.

Author

Commented:
I am selecting mine because it is part of the solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial