Question

Export Query Results to seperate Excel sheets

Asked by: bdbud

I'm looking for a way to export several different query result sets to seperate sheets all in a single Excel worksheet.
So when I click an 'Export' button, these 3 query results would be exported to a new Excel workbook, the first query going onto sheet1, the 2nd to sheet2 and so on.
Any thoughts about how I might get this to work?

Thanks in advance for any help or suggestions you might be able to offer.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-02-04 at 12:52:43ID23136196
Tags

Microsoft

,

Access

,

2003

Topics

Microsoft Access Database

,

Microsoft Excel Spreadsheet Software

,

Access Coding/Macros

Participating Experts
2
Points
250
Comments
19

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Lookup value in sheet1 from value in sheet2.
    I have a name column in sheet1 & 2, however the name column in sheet1 is "lastname,firstname middle initial" and the name column in sheet2 is "lastname, firstname (notice the space after the comma) middle initial". Is there anyway to do a lookup since...
  2. Verify “Column A” of “Sheet1” numbers are not listed i…
    Using a MS Excel Workbook with two worksheets “Sheet1” and “Sheet2”: I need to verify numbers listed in “Column A” of “Sheet1” are not listed in “Column A” of “Sheet2”. If the numbers in “Sheet1” are not listed in “Sheet2”, I need to input the current date into “Column F” cel...
  3. Need an excel macro that gets all the data in all workbook s…
    Hi, Need an excel macro that gets all the data in all workbook sheet1 to 1 new Workbook sheet. I have a folder in which i have 30 + excel files .Each file has a sheet that has data.Say 200 or 300 rows of data. Is there a way to get all these sheets data to 1 new workbook sh...
  4. Autofill rows on one sheet1 from sheet2
    Hello, I'm a super newbie to this, but what I need to do is set one sheet1 to autofill rows for every row that is on the other sheet2, basically every time a row is created in the sheet2 it needs to be copied over to sheet1... but here is the part that makes my super confused...
  5. Excel Keep/Delete Records in Sheet1 that are in Sheet2
    Please offer how I may create a new Sheet3 for the following: 1. Keep records in Sheet1 that are also in Sheet2 (Sheet2 is a subset of Sheet1 - and I want to truncate Sheet1 to only the records in Sheet2) - the result in Sheet3 will be an intersection of Sheet1 and Sheet2 2...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: etshermanPosted on 2008-02-04 at 13:24:34ID: 20818166

Straight from the Help menu ....


TransferSpreadsheet Method [Access 2003 VBA Language Reference]
The TransferSpreadsheet method carries out the TransferSpreadsheet action in Visual Basic.

expression.TransferSpreadsheet(TransferType, SpreadsheetType, TableName, FileName, HasFieldNames, Range, UseOA)

expression Required. An expression that returns one of the objects in the Applies To list.

 AcDataTransferType
AcDataTransferType can be one of these AcDataTransferType constants.
acExport
acImport default
acLink
If you leave this argument blank, the default constant (acImport) is assumed.
 

 AcSpreadSheetType
AcSpreadSheetType can be one of these AcSpreadSheetType constants.
acSpreadsheetTypeExcel3
acSpreadsheetTypeExcel4
acSpreadsheetTypeExcel5
acSpreadsheetTypeExcel7
acSpreadsheetTypeExcel8 default
acSpreadsheetTypeExcel9 default
acSpreadsheetTypeLotusWJ2 - Japanese version only
acSpreadsheetTypeLotusWK1
acSpreadsheetTypeLotusWK3
acSpreadsheetTypeLotusWK4
Note  You can link to data in a Lotus 1-2-3 spreadsheet file, but this data is read-only in Microsoft Access. You can import from and link (read-only) to Lotus .WK4 files, but you can't export Microsoft Access data to this spreadsheet format. Microsoft Access also no longer supports importing, exporting, or linking data from Lotus .WKS or Microsoft Excel version 2.0 spreadsheets by using this method.

If you leave this argument blank, the default constant (acSpreadsheetTypeExcel8) is assumed.
 

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.

FileName  Optional Variant. A string expression that's the file name and path of the spreadsheet you want to import from, export to, or link to.

HasFieldNames  Optional Variant. Use True (1) to use the first row of the spreadsheet as field names when importing or linking. Use False (0) to treat the first row of the spreadsheet as normal data. If you leave this argument blank, the default (False) is assumed. When you export Microsoft Access table or select query data to a spreadsheet, the field names are inserted into the first row of the spreadsheet no matter what you enter for this argument.

Range  Optional Variant. A string expression that's a valid range of cells or the name of a range in the spreadsheet. This argument applies only to importing. Leave this argument blank to import the entire spreadsheet. When you export to a spreadsheet, you must leave this argument blank. If you enter a range, the export will fail.

UseOA  Optional Variant. This argument is not supported.


ET

 

by: capricorn1Posted on 2008-02-04 at 13:47:45ID: 20818398

you can use this sub

Sub exportExcel()
Dim rs As DAO.Recordset
Dim xlObj As Object, Sheet As Object, iCol As Integer

    Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Add
    xlObj.Visible = True
    Set rs = CurrentDb.OpenRecordset("query1")
    Set Sheet = xlObj.activeworkbook.Worksheets("Sheet1")
        For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.range("A2").CopyFromRecordset rs  'copy the data
   
    Set rs = CurrentDb.OpenRecordset("query2")
    Set Sheet = xlObj.activeworkbook.Worksheets("Sheet2")

        For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.range("A2").CopyFromRecordset rs
       
    Set rs = CurrentDb.OpenRecordset("query3")
    Set Sheet = xlObj.activeworkbook.Worksheets("Sheet3")

        For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.range("A2").CopyFromRecordset rs
   
    'save the excel file
    xlObj.activeworkbook.saveas "C:\MyExcel.xls"
   
    Set Sheet = Nothing
    xlObj.Quit
    Set xlObj = Nothing
End Sub

 

by: bdbudPosted on 2008-02-04 at 15:10:14ID: 20819121

Thanks.  The TransferSpreadsheet action creates a new workbook for each query that is exported.  This is what I've currently got built.

Capricorn, I tried the code, but got an error:  Class does not support Automation or does not support expected interface.

Errored on:      Sheet.range("A2").CopyFromRecordset rs  'copy the data

Any thought?
Thanks again for the help!

 

by: capricorn1Posted on 2008-02-04 at 15:14:19ID: 20819151

which one? , you have three lines like that

 

by: bdbudPosted on 2008-02-04 at 15:16:50ID: 20819175

The first.

 

by: capricorn1Posted on 2008-02-04 at 15:27:15ID: 20819245

do you see the excel file created?

how does your query1 look like?

 

by: bdbudPosted on 2008-02-04 at 15:38:22ID: 20819306

It does create the Excel file and places the column headers starting at A1, thats when it stops.  Like it doesn't recognize the CopyFromRecordset action.

 

by: capricorn1Posted on 2008-02-04 at 15:40:40ID: 20819319

i just tested the codes and it run without errors.

are there any records return by your query1?

 

by: etshermanPosted on 2008-02-04 at 15:46:32ID: 20819365

>>>>>Thanks.  The TransferSpreadsheet action creates a new workbook for each query that is exported.  This is what I've currently got built.<<<<<<<

If you specify your workbook in the FileName parameter of the TransferSpreadsheet method you will establish a separate sheet within the workbook for each table/query exported.

ET

 

by: bdbudPosted on 2008-02-04 at 15:58:48ID: 20819445

Yeup, the query is returning records.  I wonder if I have to set references or something???  Something in my Excel setup might not be correct.

Capricorn, that worked.  Hmm.  If I only would have know.  Only issue is that it will transfer tables, but not query results, so I'll have to have it save the data to a table first then transfer.  This should work though for my purposes.

Thanks.

 

by: etshermanPosted on 2008-02-04 at 16:16:19ID: 20819552

>>>>>> Only issue is that it will transfer tables, but not query results, so I'll have to have it save the data to a table first then transfer.  This should work though for my purposes.<<<<<<<

You can use TransferSpreadsheet method with  a query or a table.  The query should be a Select query.

Are you getting some type of error using a quer???


ET

 

by: bdbudPosted on 2008-02-04 at 16:18:42ID: 20819563

Oh wow, this time it worked!  I wonder if I had a typo in there the first time!  Awesome, thanks again!

 

by: capricorn1Posted on 2008-02-04 at 16:20:10ID: 20819567

<Only issue is that it will transfer tables, but not query results> NOT true
it will transfer query results as recordsets

 

by: capricorn1Posted on 2008-02-04 at 16:21:44ID: 20819576

bdbud,

which one did you get to work?

 

by: bdbudPosted on 2008-02-04 at 16:23:49ID: 20819586

The Query worked.  and the table

 

by: capricorn1Posted on 2008-02-04 at 16:45:36ID: 20819720

what code did you use? can you post them here

 

by: capricorn1Posted on 2008-02-04 at 17:26:18ID: 20819918

bdbud,

are you sure you have accepted the correct suggestion?

 

by: bdbudPosted on 2008-02-05 at 10:42:47ID: 20825537

Wow.  I must be new or something...  I'm sorry, I got confused as to who was suggesting what.  I was not able to get the code that Capricorn posted to work.

Estherman's suggestion to use the TransferSpreadsheet action worked without incident.  It accomplished the task of exporting multiple query result sets to individual worksheets in a single workbook.

Thank you both for the suggestions.

 

by: capricorn1Posted on 2008-02-05 at 10:46:26ID: 20825563

np

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...