Link to home
Start Free TrialLog in
Avatar of meciab
meciabFlag for Belgium

asked on

Call a function stored as string

I want to call functions stored as string.
For example I want to automate reporting and send a data table during the night.
Before sending my report I need to refresh data table with the function UpdateMyTab.
This function is not related with an object. I just want to launch this function.
Avatar of R_Rajesh
R_Rajesh

dont what you mean exactly...  but if you paste a public fuction to a module, you should be able to launch it from anywhere by using the call statement of just typing its name
Avatar of meciab

ASKER

I've got a database with a list of reports.
For each of Those report I have to launch a function who update some data.
So for each I have a string with the name of the function I want to launch. But I d'on't know the way to do it.
Thanks
VB has support for calling method in objects by using CallByName function

Cheers
Avatar of meciab

ASKER

yes but I don't want to call method in object.
I just want a function like LaunchIt(Function as string).
for example

create standart project with form1 and form2 put a button on form1

then paste this code into form1

'form1
Private Sub Command1_Click()
CallByName Form2, InputBox("which function x or y"), VbMethod
End Sub


paste this code into form2

Public Sub x()
MsgBox "test1"
End Sub

Public Sub y()
MsgBox "test2"
End Sub

as you can see command1_click call function on form2 by string return form the input box

sorry for the solution is so simple ...


ASKER CERTIFIED SOLUTION
Avatar of phyderous
phyderous

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
u cud use the callbyname or:

select case ReportName

case Report1: Call  Make_Report1
case Report2: Call   Make_Report2
case Report3: Call   Make_Report3

end select
Avatar of meciab

ASKER

TRhe thing is that I automate a lot of things and I d'ont want to do that programaticaly and the answer of phyderous is good.
I'll put the function in a class and call with CallByName
Thanks for your help
I don't think there is an easy way to do this. You can't just call a function whoose name is in a string, becouse every function needs to be compiled before it is executed, and if you alteady have those functions defined somewhere, then you would have to write a code which could read the string, and then call the apropriate function.
For an example:

If you have several functions:    Func1(),Func2(),Func3()  etc.    then your program should do the following:

Select Case str_Function
Case "Func1()"                                        '   same as                 If str_Function = "Func1()" then Call Func1
        Call Func1

Case "Func2()"
        Call Func2

Case "Func3()"
        Call Func3
        .
        .
        .
End Select

If thoose functions needs some parameters (arguments), then it gets more complicated. Your code should be able to read the function name from a string. When it determines the name, it should determine all of the arguments from that string, and store each of them in a variable.

When all of the data is read, then it should call the apropriate function with all of the parameters.