creating excel file from access with docmd

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?
LVL 1
kw66722Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)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
0
kw66722Author 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.
0
Rey Obrero (Capricorn1)Commented:
first create a query then save it.

is this query you are building will be the same but different criteria?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

kw66722Author Commented:
They query changes based on multiple fields on the form.  There are many different combinations for the where statement.
0
Rey Obrero (Capricorn1)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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kw66722Author 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
0
Rey Obrero (Capricorn1)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.
0
Rey Obrero (Capricorn1)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
0
kw66722Author 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.
0
Rey Obrero (Capricorn1)Commented:
hmm, the DAO library should be available with version 2013.
0
kw66722Author Commented:
I am selecting mine because it is part of the solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.