Exit calling Sub

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Exit calling Sub

Post by VegasNath »

I am trying to split monster sub routines out into individual components for ease of maintenance, creating smaller routines with a master caller. If a called routine had an exit sub statement, I would also need to exit the calling sub. How would I achieve this? Would I replace the exit sub statement with some sort of GoTo handler? Is there a "best practice" approach to this type of procedure?
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 78478
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Exit calling Sub

Post by HansV »

You could change the smaller procedures (subs) to functions that return a boolean,
Return True if the function finishes normally, False if you need to quit.

Code: Select all

Function Part1() As Boolean
  ' Function will return False by default
  ...
  If ... Then
    Exit Function
  End If
  ...
  ' If we get here, return True to indicate success
  Part1 = True
End Function

Sub Whole
  ...
  If Part1 = False Then
    Exit Sub
  End If
  ...
End Sub
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12604
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Exit calling Sub

Post by StuartR »

Best practice for this is to return a status to the calling procedure, which is checked when you return. The calling procedure can then return the same error status to its caller, etc.
StuartR


User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: Exit calling Sub

Post by VegasNath »

Thanks Gents. :cheers:
:wales: Nathan :uk:
There's no place like home.....