Subject: Export Access queries to Excel
Posted By: Spymaster Post Date: 6/20/2006 2:09:23 PM
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

Reply By: bab02 Reply Date: 6/22/2006 7:32:48 PM
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

Reply By: pjm Reply Date: 8/18/2006 11:21:56 AM
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-

Go to topic 48544

Return to index page 200
Return to index page 199
Return to index page 198
Return to index page 197
Return to index page 196
Return to index page 195
Return to index page 194
Return to index page 193
Return to index page 192
Return to index page 191