DoMyWorkForMe
(1)
SDoWhat
(1)
TheGoTo
(1)
Edison
(1)
SGoto
(1)
Goto
(1)
Ooplanguages
(1)
Tiquette
(1)

Dynamic Goto

Asked By QB
20-Nov-09 12:56 PM
I have a function in which I wish to use an input variable in conjunction
with Goto, but it generates an error.  Can this done?


Function DoMyWorkForMe(sGoto as string)
Goto sGoto

Test:
Msgbox ""

End function

When I run DoMyWorkForMe("Test") and give me a label not defined ("??tiquette
non d??finie" - French PC).  Is there a way to make this work or do I need to
change my basic concept for my function?

Thank you

QB

Unfortunately, you have to change your basic concept, as the target of theGoTo

Dirk Goldgar replied to QB
20-Nov-09 02:34 PM
Unfortunately, you have to change your basic concept, as the target of the
GoTo statement cannot be evaluated at run time.  You could have a Select
Case statement:

Function DoMyWorkForMe(sDoWhat as string)

Select Case sDoWhat

Case "This"

' ... do "This" stuff ...


Case "That"

' ... do "That" stuff ...


Case "The Other"

' ... do "The Other" stuff ...

Case Else

MsgBox "What did you want to do?"

End Select

End Function

Or you could write a different public Function or Sub to perform each block
of work, pass the name of the desired procedure to function DoMyWorkForMe,
and use the Run method to call the procedure by name:

Function DoMyWorkForMe(sDoWhat as string)

Application.Run sDoWhat

End Function


--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

QB wrote:I believe you need to forget about GoTo and use Select Caseinstead.

Marshall Barton replied to QB
21-Nov-09 08:34 AM
I believe you need to forget about GoTo and use Select Case
instead.

Select Case sGoto
Case	"Test"
'do something
Case "abc"
. . .
Case Else
'Bad argument value or you fogot to add the Case for it.
End Select

--
Marsh
MVP [MS Access]

As insinuated, the Goto statement is a leftover from earlier, pre-ooplanguages

Jack Leach replied to QB
22-Nov-09 09:56 AM
As insinuated, the Goto statement is a leftover from earlier, pre-oop
languages and should be avoided.  It is used to set the error handler and the
beginning of a procedure and to redirect to an exit procedure, but should
otherwise be disregarded as a tool to use in code.

--
Jack Leach
www.tristatemachine.com

-Thomas Edison (1847-1931)
Post Question To EggHeadCafe