Prevent Error 2501 if user cancels OutputTo?

Dudley

Registered User.
Local time
Yesterday, 16:20
Joined
Apr 7, 2004
Messages
147
How can I prevent the Error 2501 - User canceled the operation error message if the user opts to hit the Cancel button on the Output To popup window where the user is to enter the filename and path of the output file? THANKS!!!

Code:
Private Sub cmdExportToExcel_Click()

    DoCmd.OutputTo acOutputQuery, "qryEXP_LSPData", acFormatXLS
 
End Sub
 
You want to add error trapping and trap that error. Searching here should turn up the code, if you're unfamiliar with it.
 
Thanks pbaldy! I'm an idiot. I'm really sorry. Here's what I put in and it's trapping it just fine:

Code:
Private Sub cmdExportToExcel_Click()
On Error GoTo ExportToExcel_Error

    DoCmd.OutputTo acOutputQuery, "qryEXP_LSPData", acFormatXLS
 
ExportToExcel_Exit:
    Exit Sub
 
ExportToExcel_Error:
    Select Case Err.Number
        Case 2501
            MsgBox "You have canceled the Export operation.", vbOKOnly, "Export Canceled"
            Exit Sub
        Case Else
            MsgBox "Error number: " & Err.Number & " has occurred. " & Err.Description, vbOKOnly, "Error"
    End Select

End Sub
 
Nothing to be sorry about. We all have the occasional brain cramp. :p
 

Users who are viewing this thread

Back
Top Bottom