Solved

Call a function stored as string

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

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

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

21 Experts available now in Live!

Get 1:1 Help Now