Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

probably dumb question

Status
Not open for further replies.

mlager

Programmer
Sep 1, 2006
74
US
Is there any way to have a sub routine do an "Exit Sub" on the sub it was called from? I.E. In the following, I would want the "Test" MsgBox not to show if the variable > 0. Effectivly, Sub Test() would terminate execution of Sub Main().

Sub Test()
If variable > 0 Then
*** EXIT SUB MAIN BELOW ***
End If
End Sub

Sub Main()
Test()
MsgBox("Test")
End Sub
 
The best way to handle this would be to have a variable.

Code:
Sub Main
m_bolOK = True
Test()
if m_bolOK then
   msgbox("test")
end sub

Sub Test()
If variable > 0 Then
   m_bolOK = False
End If
End Sub

Or something relatively similar.
 
How about using a function instead

Function Test() as Boolean
If variable > 0 Then
return false
else
return True
End If
End Sub

Sub Main()
if Test() = false then
exit sub
else
MsgBox("Test")
end if
End Sub
 
I would suggest going kliot's route with one change and that is you shouldn't exit sub when you can just allow the sub to end normally.

Code:
Function Test() as Boolean
  If variable > 0 Then
    return false
  else
    return True
  End If
End Sub

Sub Main()
  if Test() = True then
    MsgBox("Test")
  end if
End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top