How Can I Cancel A DoCmd?

D

Daniel

Hi,


I been executing a query and outputing it to Excel with
the following code:

DoCmd.OutputTo acOutputQuery, "QueryName", acFormatXLS, _
"FileName.xls", -1

This works great, but the query requires user input so if
the user decides to press cancel instead of entering the
values that the query wants, the program crashes. Is
there any way I can check if the user hit cancel? And if
the user hits cancel, how can I tell it to ignore or stop
the execution of the DoCmd...?

Thank You
 
R

Rick Brandt

Daniel said:
Hi,


I been executing a query and outputing it to Excel with
the following code:

DoCmd.OutputTo acOutputQuery, "QueryName", acFormatXLS, _
"FileName.xls", -1

This works great, but the query requires user input so if
the user decides to press cancel instead of entering the
values that the query wants, the program crashes. Is
there any way I can check if the user hit cancel? And if
the user hits cancel, how can I tell it to ignore or stop
the execution of the DoCmd...?

Generally the cancellation of any DoCmd... raises error 2501. If you add
error handling to your code and set it up to ignore error number 2501 then
you should be ok.

On Error GoTo ErrHandler

(your code goes here)

Egress:
Exit Sub

ErrHandler:
Select Case Err.Number
Case 2501
'ignore
Case Else
(you normal error handling code)
End Select
Resume Egress
 

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