Trying to exit a module

A

AJ

I have a module with multiple subs. I created an error function that I call
for a run time error.
My question is that once I get an error I want to exit the entire module. I
know I can exit the current sub I am in by "exit sub" but the program jsut
keeps processing through the rest of the code.

so if I am in a sub called from another sub, how to I kill it all??

Thanks,
 
A

akphidelt

Try to create a variable that gets changed if an error occurs...

For example call it Dim MyError as String

So it would set up like

MyError = "Error"
Exit Sub

Then when it returns to the previous sub you start it off as

If MyError = "Error" Then
MyError = Nothing
Exit Sub
Else

The rest of your code here

MyError = Nothing
End Sub

Fiddle around with it, that's usually how I do what you are trying to
accomplish.
 
A

Allen Browne

You need to write the lower level procedure so that it passes information
back to the calling procedure about whether it succeeded or not.

DoDivision() below is set up so it errors if b is zero. It passes the error
message back in the string. The main function then checks if there is
anything in the error string, and the rest of the code doesn't run after an
error.

--------code starts-------------
Public Function QuitIfGoesBad()
Dim strMsg As String
Dim dblResult As Double

strMsg = vbNullString
dblResult = DoDivision(2, 0, strMsg)

If strMsg <> vbNullString Then
MsgBox strMsg, vbExclamation, "Oops"
Else
'Put the rest of your code here
MsgBox "This part doesn't run after error"

End If
End Function

Private Function DoDivision(a As Double, b As Double, _
strErrMsg As String) As Double
On Error GoTo Err_Handler

DoDivision = a / b 'error if b is zero.

Exit_Handler:
Exit Function

Err_Handler:
strErrMsg = Err.Description
Resume Exit_Handler
End Function
--------code ends-------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top