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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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?
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.

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



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 - Microsoft MVP, Access and Data Platform) 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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