Solved

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

Posted on 1999-01-05
10
165 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now