Re: Use 'GoTo' between different subs, same module?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



> - When the called routine finishes, VBA unloads it and reloads the calling
> routine. Execution resumes at the point immediately after the calling
> statement address that was stored -- there's no way to change that,
because

And if I've used module-level variables across both routines - which I
usually do - then I could wind up somewhere unexpected with an incorrect
variable value. Now some of my head-scratching moments are starting to
clear up!

The answer, then, is what I think Jonathan and Karl are trying patiently to
explain - break routines and subroutines up into self-contained chunks that
can be fully terminated before moving into the next chunk? Am I getting
warm?

Ed

"Jay Freedman" <jay.freedman@xxxxxxxxxxx> wrote in message
news:ukVQZiOAGHA.1124@xxxxxxxxxxxxxxxxxxxxxxx
> Ed wrote:
> > "Jonathan West" <jwest@xxxxxxxx> wrote in message
> > news:uqKi4nNAGHA.3976@xxxxxxxxxxxxxxxxxxxxxxx
> >> Helmut's code causes you to jump back to the main sub at the point
> >> you left it.
> >>
> >> If you want to jump to different places depending on what happens in
> >> the called routine, then I suggest you do something like this
> >
> >> (snip)
> >
> > Jonathan and Helmut:
> > Here's what I'd like to do, if it's possible. It's jumping between
> > subs in the same module, not necessarily back to the point where I
> > left.
> >
> > Sub Main()
> > ' do stuff
> > ComeBack: '<=== want to return here
> > For Each whatever
> > 'do more stuff
> > If Len(whatever) <> 0 Then
> > 'do stuff
> > Else
> > NextSub '<=== jump to another sub in same module
> > End If
> > Next whatever
> > End Sub
> >
> > Sub NextSub()
> > For Each whosit
> > If whosit <> 0 Then
> > 'do stuff
> > Else
> > GoTo ComeBack '<== return to point in Main sub.
> > End If
> > Next whosit
> > End Sub
>
> Hi Ed,
>
> The answer in general is no, you can't do that. At least, not that way.
For
> one thing, none of the procedures "know" anything about the labels in the
> other procedures. Jonathan's suggestion is the right way to do it: make
the
> called sub into a function that returns some value depending on what
> happened inside it, and then use If or Select Case in the calling sub to
> redirect execution according to the returned value.
>
> Under the covers, here's what happens when you call a subroutine:
> - VBA saves a "snapshot" of the calling routine -- the variable values and
> the pointer to the address of the currently executing statement (the
call).
> - It loads the code of the called routine into memory and starts executing
> it at the beginning. During this phase, the calling routine is suspended,
> and not necessarily anywhere in memory.
> - When the called routine finishes, VBA unloads it and reloads the calling
> routine. Execution resumes at the point immediately after the calling
> statement address that was stored -- there's no way to change that,
because
> VBA has no mechanism to compute the address of some arbitrary label in
code
> that isn't even in memory.
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup
so
> all may benefit.
>
>
>
>


.



Relevant Pages

  • RE: Halt code without resetting
    ... Public Sub MainSub() ... ' Continue with other processing in this routine ... Private Function SubFunction1() As fnReturnValues ... ' Stop processing in this function and stop processing by calling ...
    (microsoft.public.access.modulesdaovba)
  • Re: Type mismatch error
    ... Make sure you have an error handler in your routine then it ... OK, I added an error handler, and the error is definitely in the calling ... When I put a breakpoint on every line in the sub and on the calling line, ...
    (microsoft.public.vb.general.discussion)
  • Re: Type mismatch error
    ... Make sure you have an error handler in your routine then it ... OK, I added an error handler, and the error is definitely in the calling ... When I put a breakpoint on every line in the sub and on the calling line, ...
    (microsoft.public.vb.general.discussion)
  • Re: Questions about GetRef() and ASP, etc.
    ... > Remember that functions can always be used as statements in VBS, ... - your calling routine cannot tell if that sub it is calling is a sub or a ... - once you find that it would be nice to have the routine return some status ...
    (microsoft.public.scripting.vbscript)
  • Re: How to write debug utility functions
    ... >If you are talking about Call'ing a Function or Sub, then no, the code ... >doing the calling is not active during the execution of the Function or ... >the routine that called it... ... >scope as the routine that calls it, so local variables are available to ...
    (microsoft.public.vb.general.discussion)