473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using variables as function names and calling them?

I've got a field in a table that keeps the name of functions I need to
run.

I do a DLookup on the table to retrieve the name of the function I
need to run (based on whatever criteria).

How can I get VBA to run the desired function?

I tried DoCmd.OpenFunction, I've tried using the Call command and I've
tried just sticking the variable in the code, among other things. In
most cases the variable will resolve to the proper function name, but
I will be prompted with an error saying that database can't find the
function. If I replace the variable with the function name returned,
it runs fine.

This is driving me nuts. Does anyone have a solution? I'm using
Access2002.

Oct 10 '07 #1
7 21118
ManningFan wrote:
I've got a field in a table that keeps the name of functions I need to
run.

I do a DLookup on the table to retrieve the name of the function I
need to run (based on whatever criteria).

How can I get VBA to run the desired function?

I tried DoCmd.OpenFunction, I've tried using the Call command and I've
tried just sticking the variable in the code, among other things. In
most cases the variable will resolve to the proper function name, but
I will be prompted with an error saying that database can't find the
function. If I replace the variable with the function name returned,
it runs fine.

This is driving me nuts. Does anyone have a solution? I'm using
Access2002.
Have you tried the Eval() function?

Eval(yourvar)

--
Roy-Vidar
Oct 10 '07 #2
I don't think VBA can do that in the way you want. Your asking it to edit
the program and compile on the fly. One way to simulate it, would be to
write a function using a case statement with each of the functions you want
to run in a case. Of course you would be limited to the functions programmed
into the case statement.

"ManningFan" <ma********@gmail.comwrote in message
news:11*********************@r29g2000hsg.googlegro ups.com...
I've got a field in a table that keeps the name of functions I need to
run.

I do a DLookup on the table to retrieve the name of the function I
need to run (based on whatever criteria).

How can I get VBA to run the desired function?

I tried DoCmd.OpenFunction, I've tried using the Call command and I've
tried just sticking the variable in the code, among other things. In
most cases the variable will resolve to the proper function name, but
I will be prompted with an error saying that database can't find the
function. If I replace the variable with the function name returned,
it runs fine.

This is driving me nuts. Does anyone have a solution? I'm using
Access2002.

Oct 10 '07 #3
On Oct 10, 5:26 pm, RoyVidar <roy_vidarNOS...@yahoo.nowrote:
ManningFan wrote:
I've got a field in a table that keeps the name of functions I need to
run.
I do a DLookup on the table to retrieve the name of the function I
need to run (based on whatever criteria).
How can I get VBA to run the desired function?
I tried DoCmd.OpenFunction, I've tried using the Call command and I've
tried just sticking the variable in the code, among other things. In
most cases the variable will resolve to the proper function name, but
I will be prompted with an error saying that database can't find the
function. If I replace the variable with the function name returned,
it runs fine.
This is driving me nuts. Does anyone have a solution? I'm using
Access2002.

Have you tried the Eval() function?

Eval(yourvar)

--
Roy-Vidar
Tried that. I get that same error message.

I ended up making a macro to run each function, and then I used
DoCmd.RunMacro MCR, but I was really hoping that there was a more
graceful way.

Oct 10 '07 #4
On Oct 10, 5:18 pm, ManningFan <manning...@gmail.comwrote:
I've got a field in a table that keeps the name of functions I need to
run.

I do a DLookup on the table to retrieve the name of the function I
need to run (based on whatever criteria).

How can I get VBA to run the desired function?

I tried DoCmd.OpenFunction, I've tried using the Call command and I've
tried just sticking the variable in the code, among other things. In
most cases the variable will resolve to the proper function name, but
I will be prompted with an error saying that database can't find the
function. If I replace the variable with the function name returned,
it runs fine.

This is driving me nuts. Does anyone have a solution? I'm using
Access2002.
for a sub

Sub temp()
MsgBox 1
End Sub

Sub temp2()
Dim s$
s = "temp"
Application.Run s
End Sub

----
for a function

Function temp3$(ByVal vS$)
temp3 = UCase(vS)
End Function

Sub temp4()
Dim s$
s = "temp3"
MsgBox Application.Run("temp3", "abc")
End Sub

Oct 10 '07 #5
lyle wrote:
On Oct 10, 5:18 pm, ManningFan <manning...@gmail.comwrote:
>>I've got a field in a table that keeps the name of functions I need to
run.

I do a DLookup on the table to retrieve the name of the function I
need to run (based on whatever criteria).

How can I get VBA to run the desired function?

I tried DoCmd.OpenFunction, I've tried using the Call command and I've
tried just sticking the variable in the code, among other things. In
most cases the variable will resolve to the proper function name, but
I will be prompted with an error saying that database can't find the
function. If I replace the variable with the function name returned,
it runs fine.

This is driving me nuts. Does anyone have a solution? I'm using
Access2002.


for a sub

Sub temp()
MsgBox 1
End Sub

Sub temp2()
Dim s$
s = "temp"
Application.Run s
End Sub

----
for a function

Function temp3$(ByVal vS$)
temp3 = UCase(vS)
End Function

Sub temp4()
Dim s$
s = "temp3"
MsgBox Application.Run("temp3", "abc")
End Sub
I knew someone would have an esoteric command to do the trick. Good
examples, Lyle.

Oct 11 '07 #6
On Oct 10, 9:17 pm, Salad <o...@vinegar.comwrote:
I knew someone would have an esoteric command to do the trick. Good
examples, Lyle.
I knew about and used Application.Run for many years (maybe 8 or 9).
But I neglected to follow my own preachings, "Examine methods and
properties in the Object Browser", so when I read OP's message I
failed to see how it would return values, which I felt he might want
to do, as he specified that he wanted to run a function.
When I did look in the ObjectBrowser I found that Application.Run is a
function, not a sub as I had assumed.
So, we can use it to return values from the function we "run" as
Application.Run(FunctioName,ParamArrayofFunctionAr guments).
I think this makes Application.Run way more powerful than I had
realised previously.
So OP may use the suggestion, or he may not; but I have learned
something and thats prtty nice.

Oct 11 '07 #7
ManningFan wrote:
On Oct 10, 5:26 pm, RoyVidar <roy_vidarNOS...@yahoo.nowrote:
>ManningFan wrote:
>>I've got a field in a table that keeps the name of functions I need to
run.
I do a DLookup on the table to retrieve the name of the function I
need to run (based on whatever criteria).
How can I get VBA to run the desired function?
I tried DoCmd.OpenFunction, I've tried using the Call command and I've
tried just sticking the variable in the code, among other things. In
most cases the variable will resolve to the proper function name, but
I will be prompted with an error saying that database can't find the
function. If I replace the variable with the function name returned,
it runs fine.
This is driving me nuts. Does anyone have a solution? I'm using
Access2002.
Have you tried the Eval() function?

Eval(yourvar)

--
Roy-Vidar

Tried that. I get that same error message.

I ended up making a macro to run each function, and then I used
DoCmd.RunMacro MCR, but I was really hoping that there was a more
graceful way.
This works here.

Function MyFunction1()
MsgBox "Blah!"
End Function

Sub MySub1()
Dim s As String

s = "MyFunction1()"
Eval s
End Sub

Function MyFunction2(ByVal v_s As String) As String
MyFunction2 = StrReverse(v_s)
End Function
Sub MySub2()
Dim s As String
Dim p As String

p = "Blah!"
s = "MyFunction2()"
s = Replace(s, "()", "(""" & p & """)")

MsgBox Eval(s)
End Sub

--
Roy-Vidar
Oct 11 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
12
by: Eric | last post by:
I've got a pretty large C program with global variables and function names strewn about (i.e. no "static" declarations in front of them). Now I want to expose the ability for user's to supply their...
3
by: joseluismarchetti | last post by:
Hello everybody, Although I am sure this is an important question for this group, I am not sure this question belongs to this group and I will be happy to move it to the correct one after you...
3
by: Xiaoshen Li | last post by:
Dear All, A tutorial told me that there are more than 700 functions available. To see all the function names, man 3 intro. But on my machine, it only gives one page text. For example, if I hope...
12
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
7
by: Petr Jakes | last post by:
I have got names of functions stored in the file. For the simplicity expect one row only with two function names: printFoo, printFOO In my code I would like to define functions and then to read...
5
by: Sakcee | last post by:
python provides a great way of dynamically creating fuctions calls and class names from string a function/class name can be stored as string and called/initilzed e.g def foo(a,b): return...
5
by: Maxim Veksler | last post by:
Hello list, I'm trying to write a python script that would allow me to manipulate shell variables of the calling shell. I'm trying to write some logic that would know to add LD_LIBRARY_PATH to...
2
by: david.karr | last post by:
Ok, I'm sure that subject is confusing, but I noticed the following curious code on the main page of the "Vitamin" web developer's page <http://www.thinkvitamin.com/>: <script...
16
by: Xiaoxiao | last post by:
Hi, I got a C library, is there a way to view the public function names in this library so that I can use in my C program? Thanks.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.