Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Run a function/sub based on the value of a string

Posted on 1999-01-05
10
167 Views
Last Modified: 2010-05-03
I want to create a string which is the name of a sub/function in code and run that code, eg

sFunction = "MyFunc"
AMethodofRunning sFunction

where AMethodofRunning is a function which calls the subroutine sFunction.  The question is, what is AMethodofRunning ? (In MS Access you use Application.run sFunction)
0
Comment
Question by:tomnich
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:chabaud
ID: 1454003
You cannot do that with VB6 which is compiled an not interpreted.
Or use select:

Select case sFunction
case "MyFunc1"
  MyFunc1
case "MyFunc2"
  MyFunc2
end select




0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1454004
Not. But you can do the following:
select case MyString
case "MyFunc"
      MyFunc arg
case "MyFunc1"
      MyFunc1 arg
case "MyFunc2"
      MyFunc2 arg
end select
0
 
LVL 4

Expert Comment

by:chabaud
ID: 1454005
mirkwood: thanks for you reply...
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Expert Comment

by:Mirkwood
ID: 1454007
Impossible!!!!
0
 
LVL 1

Accepted Solution

by:
tomnich earned 100 total points
ID: 1454008
Here's your answer:

vba.CallByName(MyModule,"MyFunction",VbMethod,Arg1)

MyModule is the module object containg the function, and your function name is "MyFunction", passed in as a string

From you namesake
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1454009
He, chabaud, we probably answered together. Only one minute in between
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1454010
He, funny. Two accounts for one person. Oh Linda.....
0
 
LVL 4

Expert Comment

by:mcix
ID: 1454012
In VB6, there is a new language feature named CallByName.

You might be able to trick it into doing what you want...


0
 
LVL 4

Expert Comment

by:mcix
ID: 1454014
Sorry about that...

I must have answered somewhere in the middle there...

Glad you got your answer!
0
 
LVL 4

Expert Comment

by:chabaud
ID: 1454015
to mirkwood: Impossible is not VB !!!!

We have to read the vb6 doc more deeply...
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question