Hi Antonov,
Yes, it is possible. Here is code that I use behind a command button named
"cmdExportToExcel" to do this task. The Excel file is created in the same
folder that the Access application is found in. Note: CurrentProject.Path is
valid for Access 2000 and later. If you are using Access 97, then there are
alternate methods of obtaining the path to the .mdb file.
'*****************Begin Code******************
Private Sub cmdExportToExcel_Click()
On Error GoTo ProcError
Dim strPath As String
strPath = CurrentProject.Path
DoCmd.OutputTo acOutputQuery, "qryMovieSelections", acFormatXLS, _
strPath & "\MovieSelections.xls" ', AutoStart:=-1
MsgBox "The selected movies have been exported to the file
MovieSelections.xls" & vbCrLf _
& "in the folder:" & vbCrLf & strPath, vbInformation, "Export
Complete..."
ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, , _
"Error in cmdExportToExcel_Click event procedure..."
Resume ExitProc
End Sub
'*****************End Code********************
You can also add code that disables the command button if the record count
is zero. I do this in a different procedure, since the application in
question is a QBF (Query by Form). It would be possible to add code to the
above procedure that informs the user if there are no records in the record
set, instead of disabling the command button. It just depends on how fancy
you want to get.
Tom
http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________
:
Hello, I would like to know whether it is possible to have access run a
query and export the results into excel automatically....