Question : Exporting Access Query to an Excel Spreadsheet

I have a query called "usp_Anomalies" that I would like to export to an Excel sheet. I have the following code that works and that I have attached as a code snippet. The only thing left is the export process. Please note that the data of the query must be exported including the COLUMN NAMES of the query.

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
Dim sSQL As String
  Dim objXL As Object
  Dim objWB As Object
  Dim sFile As String
  Dim sShtName As String
  Dim i As Integer
 
  'Set name of file to save to
  sFile = "C:\ANOMALIES.XLS"
 
  'Set name of new sheet
  sShtName = "ANOMALIES"
 
 
  'Create a new excel document
  Set objXL = CreateObject("Excel.Application")
 
  'create new workbook
  Set objWB = objXL.Workbooks.Add
  
  'Save as a new file, delete existing file if it exists
  If Dir$(sFile) <> "" Then Kill sFile
  objWB.SaveAs sFile
  
  objWB.Close
  objXL.Quit
 
  'CLOSE DOWN
  Set objWB = Nothing
  Set objXL = Nothing

Answer : Exporting Access Query to an Excel Spreadsheet

How about this:

    DoCmd.OutputTo acOutputQuery, [YourQuery], acFormatXLS, sFile
Random Solutions  
 
programming4us programming4us