User cancels Dialog Box in OutputTo method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using the OutputTo method to export a query to Excel. I have left the
Output File variant blank to allow the user to enter a file name and location
through a dialog box. The code is below:

DoCmd.OutputTo acOutputQuery, "qryBilledPayedComparison", acFormatXLS, , False

If the user selects the cancel option in the dialog box a run-time error
occurs. What can I do so that if the user selects the cancel option the
diolog box simply closes and the method is cancelled?
Thanks,
Jason
 
I am using the OutputTo method to export a query to Excel. I have left the
Output File variant blank to allow the user to enter a file name and location
through a dialog box. The code is below:

DoCmd.OutputTo acOutputQuery, "qryBilledPayedComparison", acFormatXLS, , False

If the user selects the cancel option in the dialog box a run-time error
occurs. What can I do so that if the user selects the cancel option the
diolog box simply closes and the method is cancelled?
Thanks,
Jason

Add error handling to the code.

On Error GoTo Err_Handler

DoCmd.OutputTo acOutputQuery, "QueryName", acFormatXLS

Exit_Sub:
Exit Sub
Err_Handler:
If Err = 2501 Then
Else
MsgBox "Error " & Err.Number & " " & Err.Description
End If
Resume Exit_Sub
 
Thank you Fred!

fredg said:
Add error handling to the code.

On Error GoTo Err_Handler

DoCmd.OutputTo acOutputQuery, "QueryName", acFormatXLS

Exit_Sub:
Exit Sub
Err_Handler:
If Err = 2501 Then
Else
MsgBox "Error " & Err.Number & " " & Err.Description
End If
Resume Exit_Sub
 
Back
Top