Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stop code running totally in called sub

Posted on 2002-07-26
7
Medium Priority
?
148 Views
Last Modified: 2010-05-02
I have a sub that calls another sub.

When an error occurs in the called sub (2nd sub) how can I make the code stop running totally and not go back to the 1st sub.

(My 2nd sub could be in the same module or in another module.)

Thanks.
0
Comment
Question by:naqayya
[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
7 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 200 total points
ID: 7179646
change the second sub to a function type boolean then do

sub1()
.
.
.
.

 if not function2() then exit sub
.
.
.
.

end sub


function2() as boolean

on error goto errhandler

.
.
.
.



function2=true
exit function
errhandler:

end function
0
 
LVL 2

Expert Comment

by:selim007
ID: 7179867
deighton  will work fine.
you can also create a public variable for example Sub2Err as long.
- then if an error occures in the second sub you'll simply put the error number in the variable:
Sub2Err=err.number
- now the program counter should return to sub1.
here you'll test if the public variable Sub2Err contains an error number, if so u'll stop processing otherwise u continue executing the code:
if Sub2Err <> 0 then  'some error occured
exit sub
end if
'continue executing sub1 code

you can use this method if you don't want your function to return any code or if your function is already  returning something and you don't wanna pass variable by reference or let the function resturns a structure.
0
 
LVL 2

Expert Comment

by:selim007
ID: 7179869
i recommend deighton method which is simple
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:rspahitz
ID: 7180388
private function sub2()
On error goto Err_Handler
...
exit sub

Err_Handler:
  if err.number = ### then
    end
  endif
end function

--
Note that the above will intercept an error and if a specified error occurs (replace ### with the desired error number) then the code will immediately stop, along with the application, and any open threads will remain open and you will lose resources...however, it will immediately stop the code as per your request!
0
 
LVL 5

Expert Comment

by:jayeshshah
ID: 7181742
you can try the below

Sub procedure1()
    call Procedure2
End sub

Sub Procedure2()
On error goto ErrHandler

'' Code in Procedure2

Exit sub
ErrHandler
     end
End sub

In case you want to do for some specific errors which occur in the procedure2 then in the Error Handler code you can write as
If Err.Number = (Error number) then
    END
else
    MsgBox Err.descrription, vbcritical
end if

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7185754
jayeshshah, please read the guidelines at the bottom of this page regarding proposed answers.  (Notice that you duplicated much of my comment.) Most experts will place all entries only as comments.
0
 

Author Comment

by:naqayya
ID: 7193736
see rspahitz's comment
0

Featured Post

Independent Software Vendors: 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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…
Suggested Courses

636 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