• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

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

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
tomnich
Asked:
tomnich
  • 4
  • 3
  • 2
  • +1
1 Solution
 
chabaudCommented:
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
 
MirkwoodCommented:
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
 
chabaudCommented:
mirkwood: thanks for you reply...
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
MirkwoodCommented:
Impossible!!!!
0
 
tomnichAuthor Commented:
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
 
MirkwoodCommented:
He, chabaud, we probably answered together. Only one minute in between
0
 
MirkwoodCommented:
He, funny. Two accounts for one person. Oh Linda.....
0
 
mcixCommented:
In VB6, there is a new language feature named CallByName.

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


0
 
mcixCommented:
Sorry about that...

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

Glad you got your answer!
0
 
chabaudCommented:
to mirkwood: Impossible is not VB !!!!

We have to read the vb6 doc more deeply...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now