Issues with docmd.openquery

I've never run into this issue before, but I created a click event that runs a qry using the following command and then exports the file.

DoCmd.SetWarnings False
DoCmd.OpenQuery "SalesByRegion", acViewNormal, acEdit
DoCmd.SetWarnings True
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "TableSbR", "C:\TableSbR.xls", True

It works, but when I run, it opens the query for viewing results instead of just running behind the scenes.  I've used thus same code before in other cases and never had it show results.

How do I get it to stop showing the query results?
vsllcAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Connect With a Mentor Commented:
From Help:

"TableName   Optional Variant. A string expression that's the name of the Microsoft Access table you want to import spreadsheet data into, export spreadsheet data from, or link spreadsheet data to, **or the Microsoft Access select query whose results you want to export to a spreadsheet ***."

So, I would say substitute the name of a Select query in place of the table ...

docmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel9,"YourTableOrQueryName" .......

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
"It works, but when I run, it opens the query for viewing results instead of just running behind the scenes. "

That's what OpenQuery does on a SELECT query.  That is the expected behavior.

You need to include the query name in the Transfer command ... in which case, the query itself will not open, but the results will be exported.

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Clarification:

"You need to include the query name in the Transfer command "

>> and remove the DoCmd.OpenQuery command.

mx
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
vsllcAuthor Commented:
My query creates the table I'm exporting.  How do I do what you suggest?
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
"My query creates the table I'm exporting"
How can a SELECT query create a table?  

mx
0
 
Jeffrey CoachmanMIS LiasonCommented:
"My query creates the table I'm exporting.
Then I am confused, ...If this is indeed an Action query (Make Table), then no results should appear on screen when the query is run.

Try it like this perhaps:

'The SQL for your Make Table query
CurrentDB.Execute "SELECT * INTO TableSbR.....",dbfailonerror
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "TableSbR", "C:\TableSbR.xls", True

JeffCoachman



0
 
vsllcAuthor Commented:
Nevermind, I miskeyed in my code.  A create table qry uses the results of the select qry.  I keyed the name of the select qry in my code.  Since the table was already created in a prev test the export worked without the create table name.

Just been working too long I guess!


0
 
vsllcAuthor Commented:
mx-still wondering though how to do your suggestion assuming I want to just export the results from my select query to excel?  I've only ever exported tables.
0
 
vsllcAuthor Commented:
Thanks!
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
You are welcome ...

mx
0
All Courses

From novice to tech pro — start learning today.