Exporting Queries

  • Thread starter Thread starter hotplate
  • Start date Start date
H

hotplate

Hello,
Is it possible to use a command button in a form to
1. Run A query, and
2. Export the Query to an Excel file always saved in the same location
and rewriting the old file?
and do this with 1 click?
Maybe I am trying to do to much?

Thanks,
James
 
It's pretty simple to do what you want:

Private Sub MyCommandButton_Click()
Dim strFile As String

' Define what file to use
strFile = "C:\Some Folder\Some File.xls"

' Delete the file if it already exists
If Len(strFile) > 0 Then
Kill strFile
End If

' Export to Excel
DoCmd.TransferSpreadsheet acExport, _
TableName := "NameOfQueryToExport", _
FileName := strFile

End Sub
 
Back
Top