Solved

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

Posted on 1999-01-05
10
164 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

759 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

17 Experts available now in Live!

Get 1:1 Help Now