Solved

Stop code running totally in called sub

Posted on 2002-07-26
7
138 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
7 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 50 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

705 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

19 Experts available now in Live!

Get 1:1 Help Now