Can I convert a control's value to a function name in code?

  • Thread starter Thread starter Tony Vrolyk
  • Start date Start date
T

Tony Vrolyk

I tried this post in the Forms group but didn't find what I was looking for
so I thought I would see if anyone here might have an idea.

1. I have a table where I store data about certain funtions. There is a
field for the function name (as it would be called in code), a f riendly
name that is displayed to the user and other fields that store info that I
use to orgnize and query the list. For instance the functions are
categorized based on where the user is in the application.

2. On a form I have a list box that displays export functions to the user
using their friendly names. The actual function name being column 1, bound
and hidden.

3. The user would choose from the list of friendly names then click Run.

4. The code behind the Run button would take the value of the list box
(which is the function name) and run that function.

It is number 4 where I am not sure what to do. Eval() is close but it is
only usefull for returning a value. In this case I am executing a Word
Automation task

The functions may also require argumants which I may get from the other
colums of the list box or the form OpenArgs but that part is easy enough.

Any ideas?
Tony
 
Thanks but I don't see how that will work.

First of all why eval the arguments? When calling a function you can already
put variables or references to controls as the arguments and they evaluate
on their own.

Secondly, Eval() specifically is for evaluating a function for a partcular
string or numeric result. The functions I am trying to run do not evaulate
to anything. They execute code that performs a Microsoft Word automation
task - like a one-off mail merge.

Sorry if you misunderstood my question or if I misunderstand your
suggestion. Feel free to clarify if need be.

Tony
 
In your example what is the purpose of the Eval function. Can't you just...
?PrpGet(CurrentDB,'Version')

If Eval worked I supposed it would look something like this
Eval("Me.lstExports(varID)")

Where Me.lstExports refers to a listbox whose bound column is a list of
user-defined function names and varID is a global variable that is passed as
an argument to the function. All the functions are a similar structure so
that they only require a single argument and that variable is set prior to
this point in the code.

Here is an example of one of the functions. I took out the error handling to
make it smaller text.

'***************************
Public Function fAssetRequest_FromClient(PlanIDNumber As String)
Dim dbC As Database, qryPlans As QueryDef
Dim recPlans As Recordset, objWord As Object
Dim objDoc As Object

Set dbC = CurrentDb()
Set qryPlans = dbC.QueryDefs("qry_Export_Plans")
qryPlans.Parameters("PID") = PlanIDNumber
Set recPlans = qryPlans.OpenRecordset()
If recPlans.EOF Then
MsgBox "ID of " & PlanIDNumber & " could not be found.", _
vbCritical, "Plan Not Found"
Exit Function
End If

Set objWord = CreateObject("Word.Application")
'Set objWord = New Word.Application
objWord.Visible = True
Set objDoc = objWord.Documents.Add("[location of word template file]")

fInsertTextAtBookmark objWord, objDoc, "A", recPlans("ContactName")
'multiple insertbookmark lines as needed

objDoc.SaveAs fReturnTempDir & "\Asset Request.doc"

End Function
'***************************

Thanks
Tony
 
Well,
I just replaced text in list box Me.lstExports with its actual value -
"PrpGet(CurrentDB,'Version')"

so if you have a listbox lstExports with functions you want to run - then
you have to call eval like:

call Eval(Me.lstExports & "(" & varID & ")")

so if you have fAssetRequest_FromClient function selected in listbox and
varID=5 then eval will run:

fAssetRequest_FromClient(5)


HTH
--
Best regards,
___________
Alex Dybenko (MVP)
http://alexdyb.blogspot.com
http://www.PointLtd.com


Tony Vrolyk said:
In your example what is the purpose of the Eval function. Can't you
just...
?PrpGet(CurrentDB,'Version')

If Eval worked I supposed it would look something like this
Eval("Me.lstExports(varID)")

Where Me.lstExports refers to a listbox whose bound column is a list of
user-defined function names and varID is a global variable that is passed
as
an argument to the function. All the functions are a similar structure so
that they only require a single argument and that variable is set prior to
this point in the code.

Here is an example of one of the functions. I took out the error handling
to
make it smaller text.

'***************************
Public Function fAssetRequest_FromClient(PlanIDNumber As String)
Dim dbC As Database, qryPlans As QueryDef
Dim recPlans As Recordset, objWord As Object
Dim objDoc As Object

Set dbC = CurrentDb()
Set qryPlans = dbC.QueryDefs("qry_Export_Plans")
qryPlans.Parameters("PID") = PlanIDNumber
Set recPlans = qryPlans.OpenRecordset()
If recPlans.EOF Then
MsgBox "ID of " & PlanIDNumber & " could not be found.", _
vbCritical, "Plan Not Found"
Exit Function
End If

Set objWord = CreateObject("Word.Application")
'Set objWord = New Word.Application
objWord.Visible = True
Set objDoc = objWord.Documents.Add("[location of word template file]")

fInsertTextAtBookmark objWord, objDoc, "A", recPlans("ContactName")
'multiple insertbookmark lines as needed

objDoc.SaveAs fReturnTempDir & "\Asset Request.doc"

End Function
'***************************

Thanks
Tony

Alex Dybenko said:
Hi,
well, perhaps you can post here what kind of expressions you have

for example you can pass expressions like:

?eval("PrpGet(CurrentDB,'Version')")

where PrpGet - is a public user-defined finction

--
Best regards,
___________
Alex Dybenko (MVP)
http://alexdyb.blogspot.com
http://www.PointLtd.com
 
Back
Top