[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

Stop code running totally in called sub

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
naqayya
Asked:
naqayya
1 Solution
 
deightonCommented:
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
 
selim007Commented:
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
 
selim007Commented:
i recommend deighton method which is simple
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
rspahitzCommented:
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
 
jayeshshahCommented:
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
 
rspahitzCommented:
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
 
naqayyaAuthor Commented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now