Solved

Stop code running totally in called sub

Posted on 2002-07-26
7
145 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 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
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!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

739 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