MS Access: export a query to Excel with Docmd.Output



https://codedocu.com/Details?d=2136&a=8&f=319&l=0

Download:

Datei 1: Access_Export_Query.accdb
Datei 2: Output_Results.xlsx


The example shows how to export an Access query to Excel.
The query contains current data from a table, which was filtered by a radio button.
For this you use the command Docmd.OutputTo in Microsoft Access
As
DoCmd.OutputTo acOutputQuery, "qryCars_Sale", acFormatXLSX, , True


When executing the OutputTo command, a Save dialog box is automatically displayed which would like to save a document in Excel.
DoCmd.OutputTo acOutputQuery, "qryCars_Sale", acFormatXLSX, , True

The acOutputQuery parameter indicates that a query is in the third parameter.

The query query is bound to the form that is currently open and uses a radio button.

If you want to output the output directly into a named Excel file, then you have to name the Excel file name with the foll path
Dim sFilename As String
    sFilename = "C:\_Daten\Desktop\Demo\Access\2018-01-02 Access Export Query\Output_Results.xlsx"
    DoCmd.OutputTo acOutputQuery, "qryCars_Sale", acFormatXLSX, sFilename, Autostart:=False


Option Compare Database
Option Explicit On


'-------< Buttons >------------
Private Sub BtnExport_Click()
    DoCmd.OutputTo acOutputQuery, "qryCars_Sale", acFormatXLSX, , True
End Sub


Private Sub btnExport_to_Filename_Click()
    Dim sFilename As String
    sFilename = "C:\_Daten\Desktop\Demo\Access\2018-01-02 Access Export Query\Output_Results.xlsx"
    DoCmd.OutputTo acOutputQuery, "qryCars_Sale", acFormatXLSX, sFilename, Autostart:=False
End Sub
'-------</ Buttons >------------


Private Sub optSelect_AfterUpdate()
    subform_Data.Requery
End Sub


Code under Visual Basic for Applications vba

Comments

Popular posts from this blog

Export Access Object to Excel ,PDF, RTF , etc. using DoCmd.OutputTo

HOW TO OPEN ACCESS IN FULL SCREEN: OPENING A FORM AS MAXIMIZED ?