ordendelfai
Technical User
I am trying to export multiple queries to the exact same Excel filename, just with different tabs. If I click on a query in Access, then go to File -> Export, and find an Excel file, then Export, Access exports the query into a new tab of the Excel file with the name of the query. I am trying to duplicate that effect using a recordset and loop.
Below is the code that exports fine, but it seems to just overwrite the excel file each time it loops. I have paused the code at each loop and checked the excel file, and the code is in fact exporting each query one by one. If I have 3 queries, only the 3rd query is in the excel file at the end. Please let me know if you have any suggestions.
A Second question, somewhat related is, can I force the output into a specific Excel version (Such as 97-2002)?
Thanks,
~Joel
As common courtesy and to help other members who might find your thread helpful in the future, please respond if the advice given resolved your issue.
Below is the code that exports fine, but it seems to just overwrite the excel file each time it loops. I have paused the code at each loop and checked the excel file, and the code is in fact exporting each query one by one. If I have 3 queries, only the 3rd query is in the excel file at the end. Please let me know if you have any suggestions.
Code:
Private Sub ExportExcel()
Dim FileName As String
Dim strSQL As String
Dim ExRst As DAO.Recordset
Dim ExportObject As String 'Holds the object to be exported
FileName = "C:\AccessOutput.xls"
strSQL = "SELECT tbl_ReportExports.* FROM tbl_ReportExports WHERE ReportID = " & ReportID & " Order By RunOrder;" 'ReportID is grabbed from a global variable
Set ExRst = db.OpenRecordset(strSQL)
If ExRst.EOF Then
MsgBox "There are no queries to export", , "No Objects"
GoTo ExitHere
End If
Do Until ExRst.EOF 'This exports all the queries to the same Excel File
ExportObject = ExRst!ExportObject
DoCmd.OutputTo acOutputQuery, ExportObject, acFormatXLS, FileName, False
ExRst.MoveNext
Loop
ExitHere:
ExRst.Close
Set ExRst = Nothing
Set exApp = Nothing
End
End Sub
Thanks,
~Joel
As common courtesy and to help other members who might find your thread helpful in the future, please respond if the advice given resolved your issue.