Solved

Call a function stored as string

Posted on 2003-10-27
9
143 Views
Last Modified: 2010-05-03
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.
0
Comment
Question by:meciab
[X]
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
9 Comments
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 9626016
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
0
 

Author Comment

by:meciab
ID: 9626048
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
0
 
LVL 5

Expert Comment

by:phyderous
ID: 9626054
VB has support for calling method in objects by using CallByName function

Cheers
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:meciab
ID: 9626069
yes but I don't want to call method in object.
I just want a function like LaunchIt(Function as string).
0
 
LVL 5

Expert Comment

by:phyderous
ID: 9626079
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 ...


0
 
LVL 5

Accepted Solution

by:
phyderous earned 500 total points
ID: 9626085
There is no diffrence in the preformance it is doing the same ...

just create class with all the function you need
0
 
LVL 7

Expert Comment

by:_agj_
ID: 9626114
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
0
 

Author Comment

by:meciab
ID: 9626132
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
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9626149
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.



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

735 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