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
239 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

786 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