Solved

Export queries to multiple excel worksheets in the same workbook using VBA.

Posted on 2006-11-21
2
938 Views
Last Modified: 2008-02-01
Hi,

I would like to export a number of access queries to different worksheets in the same workbook using Access VBA.  What's the best method?



0
Comment
Question by:geraintcollins
2 Comments
 
LVL 34

Accepted Solution

by:
jefftwilley earned 50 total points
ID: 17990274
Create a table to put your query names into and use this.

Function OutToExcel()
Dim rsOUT As DAO.Recordset
Dim strOutPutFile As String
Dim strSpecTable As String
Dim strLoc As String
Dim strCo As String

    strOutPutFile = "C:\Whatever.xls"   '<----------Your spreadsheet name

' JT Prepare to create a fresh output file by deleting any old files if they exist
    If FileExist(strOutPutFile) Then
        Kill strOutPutFile
    End If

' JT Create the Output Excel spreadsheet
    strSQL = "Select * from MyQueryTable;"  '<-----------Your query table
    Set rsOUT = CurrentDb.OpenRecordset(strSQL)
    Do Until rsOUT.EOF
        strOutQry = rsOUT!("MyQueryFieldName")    '<---------the name of the field in the query table
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, strOutQry, strOutPutFile, True
    rsOUT.MoveNext
    Loop
    rsOUT.Close
    Set rsOUT = Nothing

End Function
0
 

Author Comment

by:geraintcollins
ID: 17991835
very good - thanks mate
0

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
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…
Familiarize people with the process of utilizing SQL Server views 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 Access…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

896 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

14 Experts available now in Live!

Get 1:1 Help Now