Skip to content

Instantly share code, notes, and snippets.

@levonlee
Created October 7, 2015 21:19
Show Gist options
  • Select an option

  • Save levonlee/60119d569690d688d1d4 to your computer and use it in GitHub Desktop.

Select an option

Save levonlee/60119d569690d688d1d4 to your computer and use it in GitHub Desktop.
Export queries to Excel in Access using VBA
Option Compare Database
Option Explicit
Sub ExportToXlsx()
Dim cdb As DAO.Database, qdf As DAO.QueryDef
Set cdb = CurrentDb
Dim xlsxPath As String
xlsxPath = "C:\GP_NBM_2012.xlsx"
Set qdf = cdb.CreateQueryDef("01", _
"SELECT * FROM t1 WHERE b like '*-2012-01*'")
Set qdf = Nothing
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "01", xlsxPath, True
DoCmd.DeleteObject acQuery, "01"
Set qdf = cdb.CreateQueryDef("02", _
"SELECT * FROM t1 WHERE b like '*-2012-02*'")
Set qdf = Nothing
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "02", xlsxPath, True
DoCmd.DeleteObject acQuery, "02"
Set cdb = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment