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
241 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 125 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

705 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