Halt code without resetting

M

Mat Child

Hi,

I have a 'primary' sub (well more than one but i digress) which as part of
it's run, calls other 'secondary' subs, then continues when they have
finished.

However there are times when i don't want the primary sub to continue
running after the secondary has completed.

Previously i would simply insert an END command at the appropriate point in
the secondary sub to halt the code.

However I since i implemented a method of opening multiple instances of the
same form (From Allen Browne's excellent web-site) i can lo longer use an END
statement as it resets the VBA code causing all instances of the multiple
form to close.

Is there any other command to exit or halt the code without resetting it or
returning control to the previous sub?
 
G

Gary Brown

What I typically do in situations like this is to use a global Booleen
variable.
I originally set it to TRUE.
In the code right after I've called a procedure, I check to see if the
variable is still true (may have been changed by the sub-procedure to FALSE).
If it's still TRUE, I continue on. If it's FALSE, I go to exit_Sub.
 
C

Clifford Bass

Hi Mat,

I would suggest the use of an enumeration and functions instead of
subs. Each function would return a value that would tell the calling
function (or subroutine) what to do. This will eliminate the crashing of the
code that you are experiencing. This will also eliminate the problems of
using one global variable, which with multiple routines may result in
subroutines halting when you want to halt at only one level. It will also
eliminate the problem of the setting of one global variable by one instance
of the form interfering with other instances of the same form.

Public Enum fnReturnValues
fnrvError
fnrvContinue
fnrvHalt
End Enum

Public Sub MainSub()

Dim fnrvStatus As fnReturnValues

fnrvStatus = SubFunction1
Select Case fnrvStatus
Case fnrvContinue
' Continue with other processing in this routine
Case fnrvHalt
' Do nothing
Case fnrvError
' Processing related to error
End Select

End Sub

Private Function SubFunction1() As fnReturnValues

Dim fnrvStatus As fnReturnValues

On Error GoTo Handle_Error

' Some processing
If SomeCondition Then
' Stop processing in this function and stop processing by calling
routine
SubFunction1 = fnrvHalt
Else
' Other processing including call to another function
fnrvStatus = SubFunction2
Select Case fnrvStatus
Case fnrvContinue
' Other processing in this function and then continue in
calling routine
SubFunction1 = fnrvContinue
Case fnrvHalt
' Stop processing in this function, but continue in calling
routine
SubFunction1 = fnrvContinue
Case fnrvError
' Processing related to error, and do error processing in
calling routine
SubFunction1 = fnrvError
End Select
End If

Exit_Function:
Exit Function

Handle_Error:
SubFunction1 = fnrvError
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Function

End Function

Private Function SubFunction2() As fnReturnValues

On Error GoTo Handle_Error

' Some processing
If OtherCondition Then
' Stop processing in this function and stop processing by calling
routine
SubFunction2 = fnrvHalt
Else
' Other processing
SubFunction2 = fnrvContinue
End If

Exit_Function:
Exit Function

Handle_Error:
SubFunction2 = fnrvError
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Function

End Function

Hope that helps,

Clifford Bass
 
M

Mat Child

Thnaks Guys

I'd already considered boolean functions, but was hoping for something as
simple as the end command, some of these can get upto 2 or 3 levels deep.

Mat
 

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