User cancels Dialog Box in OutputTo method

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
 
F

fredg

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top