Solved

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

Posted on 2006-11-21
2
960 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

820 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