Eval function with variables

tuna

Registered User.
Local time
Today, 07:34
Joined
Mar 31, 2010
Messages
27
Hi, is it possible to use the eval function with variables e.g. Eval("foo + bar") or Eval("""The value of foo is "" & foo")?

This is because I have my database settings in a table (one field having the name of the setting and the other containing the value) and I wish to be able to reference variables in the settings value. E.g., for the "MsgOnExit" setting, I would have the string

"Are you sure you would like to exit " & AppTitle

Where AppTitle is the name of the database. I would then plan to use Eval at runtime to evaluate the expression to resolve AppTitle to a value. Of course, this may not be the best example as many would argue AppTitle should be a constant, but there are other settings which rely more heavily on variables being able to be resolved at runtime.

(I'm using a settings table because it allows for a settings GUI and also the ability to variables across sessions.)

Thanks
 
Msgbox "Are you sure you would like to exit " & AppTitle

will work without any eval what so ever.... so why use it
 
Thanks for your reply. It won't work because that's the literal string that will be stored in the settings table:

Code:
NAME      | VALUE
MsgOnExit | "Are you sure you would like to exit " & AppTitle

So really, I will end up with the string """Are you sure you would like to exit "" & AppTitle"

As I said, I'm doing this because I want to be able to reference variable in the settings value.
 
I would probably do something like:
replace ("Are you sure you would like to exit [AppTitle]", "[AppTitle]","program name")

To the best of my knowledge you cannot use eval for something like this
 
Thanks - but what about a setting like "var1 * var2"
 
Y = 5
Z = 10
Eval ( "Y * Z") << Will fail, because Eval requires functions...

Function y() As Integer
y = 5
End Function
Function Z() As Integer
Z = 10
End Function

eval("y() * z()")
50

Will work....
 
Hmmm. Ok, but what if I have >200 variables? Does this mean that I have to create >200 functions to call each variable?

Could I create just one function to reference variables dynamically? E.g.

Code:
Function VarGet(pVarName as String) As Variant
   'Code here - returns value of variable named according to the value of pVarName
End Function

Does VBA support dynamic variable references? I had wanted to create variable dynamically at runtime a while ago but was unable to do so. Thanks
 

Users who are viewing this thread

Back
Top Bottom