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
238 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need VBS version of Excel date conversion 4 64
using Access 8 58
Add and format columns in vb6 7 45
how to know if my Checkbox is True in VB6.0? 9 36
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now