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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

760 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

21 Experts available now in Live!

Get 1:1 Help Now