473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,378 developers and data experts.

How to Cancel a Sub and pass it on to the Calling Sub

kdsdata
Introduction
There are a number of (good) examples of how to cancel via forms, controls and msgbox(s). However, there is often a need to execute a cancel that carries up through calling subs. Here is an example of 2 subs that do just that. The example is writen in a module in 2003 Excel VBA.

Details
First a global variable bolCancel is declared in the Declaration Section. Then the main sub is executed, and the bolCancel is initialized with a "no" cancel condition.

Declaration Section
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. 'declare bolCancel as a global variable
  4. Public bolCancel As Boolean
  5.  
The main sub topSub is intended to show that it executes some code. A msgbox is used to simulate some code for the basis of the decision to cancel or to proceed execution of code.

topSub
Expand|Select|Wrap|Line Numbers
  1. Public Sub topSub()
  2. Dim choice As Integer
  3.  
  4.     'initialize Cancel, do this first
  5.     bolCancel = False
  6.  
  7.     'could execute some code here
  8.  
  9.     'to test without code
  10.     'msgbox is used to simulate Cancel/No-cancel
  11.     choice = MsgBox("1. topSub," & _
  12.         vbCr & "Click OK to test NO cancel in this topSub," & _
  13.         vbCr & "Click Cancel to test YES cancel", vbOKCancel)
  14.  
  15.     'chech the cancel condition by some code above
  16.     Select Case choice  'choice could be created by any other code
  17.         Case 1  'ok (return code by msgbox)
  18.             'call some other sub
  19.             MsgBox "2. topSub," _
  20.                 & vbCr & "this topSub has NO cancel condition," & _
  21.                 vbCr & "and will call SomeOtherSub now", vbOKOnly
  22.             someOtherSub
  23.         Case 2  'Cancel (return code by msgbox)
  24.             'this topSub code has a cancel condition
  25.             MsgBox "3. topSub," & _
  26.                 vbCr & "this topSub has a cancel condition," & _
  27.                 vbCr & "and will cancel/exit now", vbOKOnly
  28.             Exit Sub
  29.     End Select
  30.  
  31.     'check if Sub Call returned a Cancel
  32.     Select Case bolCancel
  33.         Case False  'No Cancel
  34.             'call some other sub
  35.             MsgBox "4. topSub," & _
  36.                 vbCr & "SomeOtherSub did NOT return Cancel," & _
  37.                 vbCr & "and is executed normally through here", vbOKOnly
  38.             'execution continues after "end select"
  39.         Case True  'Yes Cancel
  40.             'call some other sub
  41.             MsgBox "5. topSub," & _
  42.                 vbCr & "SomeOtherSub did return Cancel," & _
  43.                 vbCr & "and is aborted through here", vbOKOnly
  44.             'therefore we can also cancel the topSub, if necessary
  45.             Exit Sub
  46.     End Select
  47.  
  48.     'remaining code here
  49.     MsgBox "7. topSub," & _
  50.         vbCr & "this topSub has executed normally," & _
  51.         vbCr & "and will exit now", vbOKOnly
  52.  
  53. End Sub
  54.  
From the main sub topSub we go two ways.

On a "yes" cancel in topSub, there is no need to use bolCancel, just an Exit Sub is sufficient to abort.

A "no" cancel just allows the code to continue in topSub by calling someOtherSub.

someOtherSub
Expand|Select|Wrap|Line Numbers
  1. Sub someOtherSub()
  2. Dim response As Integer
  3.  
  4.     'execute some code
  5.  
  6.     'to test without code
  7.     'the message box simulates code that causes a condition,
  8.     'from where you want to Cancel the call to SomeOtherSub
  9.     response = MsgBox("8. SomeOtherSub," & _
  10.         vbCr & "Click OK to test NO cancel in this SomeOtherSub," & _
  11.         vbCr & "Click Cancel to test YES cancel", vbOKCancel)
  12.  
  13.     'chech what was returned by the message box, or some other code above
  14.     Select Case response
  15.         Case 1  'ok
  16.             'execute some other code
  17.             'execution continues after "end select"
  18.         Case 2  'Cancel
  19.             'cancel and return to calling sub
  20.             MsgBox "9. SomeOtherSub," & _
  21.                 vbCr & "this SomeOtherSub has a cancel condition," & _
  22.                 vbCr & "and will cancel/exit now", vbOKOnly
  23.             bolCancel = True
  24.             Exit Sub
  25.     End Select
  26.  
  27.     'other code here
  28.     MsgBox "10. SomeOtherSub," & _
  29.         vbCr & "after executing code," & _
  30.         vbCr & "this SomeOtherSub is returning normally", vbOKOnly
  31.  
  32. End Sub
  33.  
In the called SomeOtherSub we also have code to simulate a cancel/no-cancel conditions. However here, on a cancel request, the global bolCancel is set to True and the sub is exited/aborted.

When SomeOtherSub returns, topSub can check for a cancel request and act accordingly.

Conclusion
There are no doubt other ways of achieving the same purpose. The intent here is to show an example with more detail and possible methods for exiting/aborting. I have fond (no criticism intended) other examples often exclude details, as they are meant to be intuitively obvious. To me, "intuitively obvious" never is. Hope this helps some of you.
Dec 30 '10 #1
0 2870

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Todd | last post by:
Is it possible to pass an argument by reference through an event in C++ managed code? For example, can I do the following? public __gc class MyClass : public Control { public: __event...
1
by: Maurice Mertens | last post by:
Hi all, I want to cancel the right-mouse click on a control in VB.NET. Does anyone know how to do this? -- Met vriendelijke groet / With regards / Saludos, Moviat Automatisering
5
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer)...
1
by: Dalan | last post by:
Although I have used the code below to suppress the DoCmd cancel action message from appearing on lots of other actions such as preview and print, I have not been able to get to work on a...
4
by: Paul T. Rong | last post by:
Hello there, I have tried all day but in vain....here is the problem: Form1: there are 50 labels (so far, the number is subject to change), each label caption is a fruit or a vegetable, for...
6
by: allyn44 | last post by:
HI--what I am trying to do is 2 things: 1. Open a form in either data entry mode or edit mode depending on what task the user is performing 2. Cancel events tied to fields on the form if I am in...
6
by: Peter M. | last post by:
Hi all, If an event has multiple subscribers, is it possible to cancel the invocation of event handlers from an event handler? Or to be more specific: I'm subscribing to the ColumnChanging...
1
by: spaulsamin | last post by:
Hi Please let me know how to do the following here, i have property and event in a class, now i want to fire the event from the property when i get value, how to pass args for the below event...
13
by: Steve | last post by:
On page 392 of "Javascript the definitive guide" a function is called like this:- <form action="processform.cgi" onsubmit="return validateForm();"> Why, in this instance, is the return...
16
by: parez | last post by:
I start a BackGroundWorker to populate a grid. It is started off in the ui layer The thread follows( cannot think of a better word) the path UI->Layer1->Layer2->Communication Layer and it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.