Terminate main procedure from a subroutine?

  • Thread starter Thread starter Jay
  • Start date Start date
J

Jay

Hi -

Can a main procedure that calls a subroutine be terminated from within the
subroutine?

For example, the following demonstrates a main procedure calling a
subroutine that opens the FileDialog object. If the user presses [Cancel]
without selecting a file, the subroutine executes the 'Exit Sub' statement
and control passes back to the main procedure where execution continues. Is
there a replacement statement for the Exit Sub that would terminate the
subroutine and the calling procedure at the same time?
----------
Thanks,
Jay

Sub main()
mySubroutine
test = 1
End Sub

Sub mySubroutine()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
If .Show = -1 Then
strSourceFile = .SelectedItems(1)
Else
MsgBox "No input file selected; import canceled."
Exit Sub
End If
End With
End Sub
------------
 
The short answer is no. Your example is difficult to understand as it doesn't
do anything except call a separate subroutine to get a filename that never
gets returned and doesn't do anything with it.

If I read more into your example I would assume you were using your
subroutine to get a filename and then doing something with it back in the
main procedure. In this case you would probably change your subroutine to a
function that returns a filename as s string. In the case the user cancels
you can return an empty string ("") and check for a it upon return of control
to main. If no filename is retunred you would exit main.

Am I understanding you?

Eric
 
Jay said:
Can a main procedure that calls a subroutine be terminated from within the
subroutine?

For example, the following demonstrates a main procedure calling a
subroutine that opens the FileDialog object. If the user presses [Cancel]
without selecting a file, the subroutine executes the 'Exit Sub' statement
and control passes back to the main procedure where execution continues. Is
there a replacement statement for the Exit Sub that would terminate the
subroutine and the calling procedure at the same time?

Convert that subroutine to a function call with a variant data type.
Then pass back the value present in strSource. If that value is null
or a zero length string then deal with it accordingly in the main
subroutine.

Tony

--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 
Thanks EJ. I typically do use the technique you suggest. I have a battery
of functions and subroutines that will need to be called in succession and I
thought maybe I could save a line of code for each. However, the approach
you suggest is minimalist and does ensure that the main procedure also
terminates naturally. I'll stick with that approach.

Thanks again,
Jay

EJ Williams said:
The short answer is no. Your example is difficult to understand as it doesn't
do anything except call a separate subroutine to get a filename that never
gets returned and doesn't do anything with it.

If I read more into your example I would assume you were using your
subroutine to get a filename and then doing something with it back in the
main procedure. In this case you would probably change your subroutine to a
function that returns a filename as s string. In the case the user cancels
you can return an empty string ("") and check for a it upon return of control
to main. If no filename is retunred you would exit main.

Am I understanding you?

Eric

Jay said:
Hi -

Can a main procedure that calls a subroutine be terminated from within the
subroutine?

For example, the following demonstrates a main procedure calling a
subroutine that opens the FileDialog object. If the user presses [Cancel]
without selecting a file, the subroutine executes the 'Exit Sub' statement
and control passes back to the main procedure where execution continues. Is
there a replacement statement for the Exit Sub that would terminate the
subroutine and the calling procedure at the same time?
----------
Thanks,
Jay

Sub main()
mySubroutine
test = 1
End Sub

Sub mySubroutine()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
If .Show = -1 Then
strSourceFile = .SelectedItems(1)
Else
MsgBox "No input file selected; import canceled."
Exit Sub
End If
End With
End Sub
------------
 
Thanks Tony. See my reply to EJ; it applies to your response, too. I'll
stick with the approach that you and EJ suggest.

Thanks again,
Jay

Tony Toews said:
Jay said:
Can a main procedure that calls a subroutine be terminated from within the
subroutine?

For example, the following demonstrates a main procedure calling a
subroutine that opens the FileDialog object. If the user presses [Cancel]
without selecting a file, the subroutine executes the 'Exit Sub' statement
and control passes back to the main procedure where execution continues. Is
there a replacement statement for the Exit Sub that would terminate the
subroutine and the calling procedure at the same time?

Convert that subroutine to a function call with a variant data type.
Then pass back the value present in strSource. If that value is null
or a zero length string then deal with it accordingly in the main
subroutine.

Tony

--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 
Back
Top