Solved

Call a function stored as string

Posted on 2003-10-27
9
137 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
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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now