Solved How to execute a variable that holds the name of a function? (1 Viewer)

KitaYama

Well-known member
Local time
Today, 08:17
Joined
Jan 6, 2022
Messages
1,540
I have a string variable that holds the name of a function.
I can run the function this way :
Code:
Sub test()
    Action = "fEmergencies"
    Run (Action)
End Sub

Function fEmergencies(Optional sit As Boolean = False, Optional Trigger As String = "frmSwitchBoard")
   
    ' Do something here

end Funcion

How can I run the function and pass a true for sit argument.
This line gives me Compile Error.
Code:
Sub test()
    Action = "fEmergencies"
    Run Action(True)
End Sub

Thanks for any kind of advice.
 
Solution
for a test.
but Eval wont accept if there is second Parameter and no 1st parameter, like:

Action = "fEmergencies(, 'myswichBoard')"

Code:
Sub test()
    Dim Action As String
    Action = "fEmergencies()"
    Eval Action
    
    Action = "fEmergencies(True)"
    Eval Action
    
    Action = "fEmergencies(true, 'myswichBoard')"
    Eval Action
End Sub

Function fEmergencies(Optional sit As Boolean = False, Optional Trigger As String = "frmSwitchBoard")
  
    ' Do something here
    MsgBox "sit = " & sit & ", Trigger = " & Trigger
End Function

KitaYama

Well-known member
Local time
Today, 08:17
Joined
Jan 6, 2022
Messages
1,540
Why do you want to do that as opposed to just calling the function?
Sorry, I can't understand what you mean?
If you mean why I save the name of the function in a variable, it's a long story.
To make it short, The string (Action) is the result of a DLookup to extract the function based on user privileges and is used for different items and buttons from the ribbon.
 
Last edited:

KitaYama

Well-known member
Local time
Today, 08:17
Joined
Jan 6, 2022
Messages
1,540
Check out the CallByName() function.
CallByName's first argument is object (Required: Variant (Object). The name of the object on which the function will be executed.)

What should I use for object? I only want to run a function in a module.
 

KitaYama

Well-known member
Local time
Today, 08:17
Joined
Jan 6, 2022
Messages
1,540
Eval("fEmergencies(True)")
I've already checked eval.
It should be the variable. not the name of the function.
Something like :
Eval(Action(True))
But I received compile error.

There's no return value for the function. It can be changed to sub.
During the evolution of the database, there's been a lot of changes. Previously there's been a return value. But not now.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:17
Joined
May 7, 2009
Messages
19,229
for a test.
but Eval wont accept if there is second Parameter and no 1st parameter, like:

Action = "fEmergencies(, 'myswichBoard')"

Code:
Sub test()
    Dim Action As String
    Action = "fEmergencies()"
    Eval Action
    
    Action = "fEmergencies(True)"
    Eval Action
    
    Action = "fEmergencies(true, 'myswichBoard')"
    Eval Action
End Sub

Function fEmergencies(Optional sit As Boolean = False, Optional Trigger As String = "frmSwitchBoard")
  
    ' Do something here
    MsgBox "sit = " & sit & ", Trigger = " & Trigger
End Function
 
Solution

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:17
Joined
Oct 29, 2018
Messages
21,454
CallByName's first argument is object (Required: Variant (Object). The name of the object on which the function will be executed.)

What should I use for object? I only want to run a function in a module.
There's a sample here. See if it helps...

 

KitaYama

Well-known member
Local time
Today, 08:17
Joined
Jan 6, 2022
Messages
1,540
for a test.
but Eval wont accept if there is second Parameter and no 1st parameter, like:

Action = "fEmergencies(, 'myswichBoard')"

Code:
Sub test()
    Dim Action As String
    Action = "fEmergencies()"
    Eval Action
   
    Action = "fEmergencies(True)"
    Eval Action
   
    Action = "fEmergencies(true, 'myswichBoard')"
    Eval Action
End Sub

Function fEmergencies(Optional sit As Boolean = False, Optional Trigger As String = "frmSwitchBoard")
 
    ' Do something here
    MsgBox "sit = " & sit & ", Trigger = " & Trigger
End Function
@arnelgp You're genius.
Million thanks.
 

KitaYama

Well-known member
Local time
Today, 08:17
Joined
Jan 6, 2022
Messages
1,540
There's a sample here. See if it helps...

Thanks for the sample database.
But the sub is not running in the context of a form. Me (as a reference to the form) can not be used.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 00:17
Joined
Sep 12, 2006
Messages
15,641
Eval should work, but it's a question of getting the syntax correct.
The function that you are evaluating must know what it's source is. You don't have to pass the source to the eval, although I am sure you can.
If the source is always a certain query you can set up the parameters for that query before calling the "eval"

set query parameters
set query name "somequery"
decide which function
eval ("function name")

function name will then be able to operate on "somequery".

Something like that.
 

KitaYama

Well-known member
Local time
Today, 08:17
Joined
Jan 6, 2022
Messages
1,540
Eval should work, but it's a question of getting the syntax correct.
The function that you are evaluating must know what it's source is. You don't have to pass the source to the eval, although I am sure you can.
If the source is always a certain query you can set up the parameters for that query before calling the "eval"

set query parameters
set query name "somequery"
decide which function
eval ("function name")

function name will then be able to operate on "somequery".

Something like that.
@gemma-the-husky

I appreciate taking your time and trying to help, but unfortunately I can't understand what you mean. It's possibly because of my lack of knowledge in Access and English.
I don't have any query and I don't need to run the function by running a query.
In my case, Dlookup returns a string value (the name of a function) and I needed to run the function and pass a parameter.
For now @arnelgp 's method works perfectly.

Thanks again for your input.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 00:17
Joined
Sep 12, 2006
Messages
15,641
I meant that the function you are running might need to obtain some external information, and although you can pass in that information inside the eval, (and that's the best way) you could achieve the same result in other ways.
 

Users who are viewing this thread

Top Bottom