The Cancel Button

blueshark

Board Regular
Joined
Mar 21, 2003
Messages
54
What is the proper way to use the Cancel button on pop up windows dialog box (e.g. Save, Open)? Whenever I click Cancel my code breaks.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
In most cases the return value will = FALSE, so you probably want to check for that.
 
Upvote 0
When you choose CANCEL, Access generates Error 2501.
You need to trap this error and deal with it.
This code allows the user to return to the lstboxReports form
when the user clicks CANCEL. This is better than letting the user
get to the DEBUG Dialog box.


Private Sub lstbxReports_Click()
On Error GoTo err_NoData
DoCmd.OpenReport Me![lstbxReports], acViewPreview
exit_err_NoData:
Exit Sub

err_NoData:
If Err.Number = 2501 Then
Resume exit_err_NoData
Else
MsgBox Err.Description
Resume exit_err_NoData
End If
End Sub
 
Upvote 0
This is a more comprehensive explanation...

Trapping Error Code 2501

Use this code when you give the user the option to CANCEL the operation after he launches a command to open a report; typically when the user is asked to enter a start date and / or end date paramenter for the query that the report is based on. If the user chooses CANCEL when asked for a date, the user will be taken to a DEBUG dialog box if the code below (or equivalant) is not attached to the cmdOpenReport_Click() event.

Error code 2501 is "trappable" so all you need to do is to add error handling to the form event that opens the report ... something like this:

**************************

Private Sub cmdOpenReport_Click()

On Error GoTo err_NoData

... Add any other code you have here.

DoCmd.OpenReport "your report name goes here', acViewPreview
DoCmd.Maximize

exit_err_NoData:
Exit Sub

err_NoData:
If Err.Number = 2501 Then
Resume exit_err_NoData
Else
MsgBox Err.Description
Resume exit_err_NoData
End if
End Sub
 
Upvote 0
Exactly what I needed. I need to work on my error handling skill now that other people are starting to use my databases.
 
Upvote 0
Here's the error checking structure I use. It's a little more robust then John's if then example.

Sub Test()
On Error Goto ErrorHandler

Your code


Exit Sub

ErrorHandler:
Select Case err.number

Case 2501
Msgbox "You have cancelled the dialog"
Exit Sub

Case 2502
Msgbox "This is just an example"
Exit Sub

Case Else
Msgbox err.number & " " & Err.description

End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,351
Members
451,697
Latest member
pedroDH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top