Issues with docmd.openquery

Posted on 2010-11-16
Medium Priority
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?
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

771 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