Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2006, 02:09 PM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Export Access queries to Excel

I have 14 Access 2000 queries that I wish to export to a single Excel 2000 Worksheet with the output of each query being exported to a separate worksheet within the workbook. Is this possible from within Access and if so how is it done?

Thanks

Craig

 
Old June 22nd, 2006, 07:32 PM
Authorized User
 
Join Date: Jul 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Private Sub CmdTrns_Click()

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query2", "C:\Documents and Settings\Owner\Desktop\MySpreadsheet.xls", , "Sheet2"

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query3", "C:\Documents and Settings\Owner\Desktop\MySpreadsheet.xls", , "Sheet3"

End sub

 
Old August 18th, 2006, 11:21 AM
pjm pjm is offline
Authorized User
 
Join Date: Jul 2006
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My interpretation of your question is that you want to place the the SQL code for all
your queries onto one sheet and make a separate sheet for each of the resulting tables
from these queries. My code for this is below. Note that I create a brand new Excel file
and that there are certain caveats that I listed near the bottom of the code.

Public Sub QueryToExcel(ExcelFile As String)
Dim db As Database
Dim qdf As QueryDef

Dim qryCount As Integer
Dim SheetName As String

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)

    'Make the worksheet visible.
    xlSheet.Application.Visible = True

    Set db = CurrentDb()

    With db
        qryCount = 0
        For Each qdf In .QueryDefs
            qryCount = qryCount + 1
            ' Output qryCount, .Name and .SQL to the next line
            ' in a sheet in the Excel file
            With xlSheet.Cells(qryCount, 1)
                .Value = Str(qryCount)
                .Font.Size = 12
                .Font.Bold = True
            End With
            With xlSheet.Cells(qryCount, 2)
                .Value = qdf.Name
                .Font.Size = 12
                .Font.Bold = True
            End With
            With xlSheet.Cells(qryCount, 3)
                .Value = qdf.SQL
                .Font.Size = 10
            End With
        Next qdf
    End With

    'Save the new worksheet, close Excel and clear the object variable.
    xlSheet.SaveAs ExcelFile
    xlSheet.Application.Quit
    Set xlSheet = Nothing

    ' Create new sheets in the newly created Excel file for
    ' the results of each of the queries
    With db
        qryCount = 0
        For Each qdf In .QueryDefs
            With qdf
                qryCount = qryCount + 1
                SheetName = "Query" & Trim(Str(qryCount))

                ' Create a new sheet in the specified Excel file with
                ' the output of the named query.
                ' If there is a pre-existing one with the same name
                ' the new sheet will have a "1" appended to its name.

                ' Problems or issues:
                ' 1. Invalid queries will error out
                ' 2. Action queries will error out
                ' 3. Queries with parameters will require user input

                DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
                    .Name, ExcelFile, , SheetName
            End With
        Next qdf
        .Close
    End With

End Sub

-Phil-





Similar Threads
Thread Thread Starter Forum Replies Last Post
Export an Access query to Excel Peter Martin Access 5 June 8th, 2006 06:36 AM
Queries export to excel ymeyaw Access VBA 0 April 21st, 2006 01:17 AM
Access and Excel Queries in VB Mihai B Access VBA 1 January 24th, 2005 08:40 AM
Exported Access Parameter Queries to Excel vallasca Access VBA 4 January 19th, 2005 07:34 PM
Export Table from Access to Excel aramchan Access VBA 2 July 12th, 2004 03:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.