Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Detecting a Cancel Button Press-- Where does the Value of Err.Number from the CommonDialog.ShowOpen come from?

Posted on 2006-06-19
4
Medium Priority
?
243 Views
Last Modified: 2010-04-30
Hello,
I'm modifying a VB5 app to allow it to ask the user to save a file or to cancel.

After calling CommonDialog1.ShowOpen, I'd like to know if the user clicked on the cancel button.

I've seen answers that sets the CommonDialog1.CancelError=True, calls the ShowOpen method, then checks for Err.Number (see below for example -- excerpt from another response):

Private Sub Command1_Click()
    On Error Resume Next
    CommonDialog1.CancelError = True
    'CommonDialog1.FileName = ""
    CommonDialog1.ShowOpen
    If Err.Number > 0 Then
        MsgBox "Cancel is Clicked"
    Else
        MsgBox "OK is Clicked"
    End If
End Sub

My question is: Do I have to declare the Err as an Integer somewhere else in the code prior to the CommonDialog1.ShowOpen code, or it's automatically declared and populated by the ShowOpen? I have a scenario similar to above and I keep on getting Err.Number = 0 even if I clicked on the 'Cancel' button.

Also, what does "On Error Resume Next" mean?

Thanks and looking forward to anyone's help.

VBUserCA

0
Comment
Question by:klow5171
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 16940058
Err is VB's error object. whenever errors occur in VB program Err object is created by VB runtime. No need to do any declarations.

on error resume next means, It indicates in case error occurs on current statement just ignore the error and proceed to next statement. its better to avoid this type of coding instead use on error goto statement. you can modify the example code as follows

Private Sub Command1_Click()
    On Error Goto ErrExit
    CommonDialog1.CancelError = True
    'CommonDialog1.FileName = ""
    CommonDialog1.ShowOpen

    SafeExit:
           Exit sub
    ErrExit:
    If Err.Number = cdlCancel Then
        MsgBox "Cancel is Clicked"
    Else
        MsgBox "OK is Clicked"
    End If
     
    resume SafeExit

End Sub
0
 
LVL 39

Expert Comment

by:appari
ID: 16940062
small change in the code

Private Sub Command1_Click()
    On Error Goto ErrExit
    CommonDialog1.CancelError = True
    'CommonDialog1.FileName = ""
    CommonDialog1.ShowOpen

    MsgBox "OK is Clicked"

    SafeExit:
           Exit sub
    ErrExit:
    If Err.Number = cdlCancel Then
        MsgBox "Cancel is Clicked"
    End If
     
    resume SafeExit

End Sub
0
 
LVL 39

Expert Comment

by:appari
ID: 16940071
>>I have a scenario similar to above and I keep on getting Err.Number = 0 even if I clicked on the 'Cancel' button.
i think the commondialogs cancelerror is set to false. if cancelerror is false even if we click cancel button it wont raise the error. try to set cancelerror to true and check again
0
 

Author Comment

by:klow5171
ID: 16944013
Hello,
The "On Error..." approach worked. Thanks a lot, especially for the explanation on the Err value.

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

671 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question