Issues with docmd.openquery

Posted on 2010-11-16
Last Modified: 2012-05-10
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?
Question by:vsllc
  • 5
  • 4
LVL 75
ID: 34149576
"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.

LVL 75
ID: 34149583

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

>> and remove the DoCmd.OpenQuery command.


Author Comment

ID: 34149635
My query creates the table I'm exporting.  How do I do what you suggest?
LVL 75
ID: 34149716
"My query creates the table I'm exporting"
How can a SELECT query create a table?  

LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 34149735
"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


Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.


Author Comment

ID: 34149844
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!


Author Comment

ID: 34149941
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.
LVL 75

Accepted Solution

DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
ID: 34149986
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" .......


Author Comment

ID: 34157034
LVL 75
ID: 34157047
You are welcome ...


Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now