QueenKirsty
Registered User.
- Local time
- Today, 14:06
- Joined
- Mar 13, 2009
- Messages
- 31
Newbie to the forum here so sorry if this post is not totally clear!
I am trying to get the value of a variable by evaluating a string which matches the variable name.
As part of a State Machine I have a list of function names (delimited with %) that I want to run. The function string contains variable names. I want to use eval() to run the function but it cant convert the variable names in the string into their respective values from the sub that is calling eval().
To counter this I want to pull each variable out in turn, convert it to its value and then put it back in the string. The problem is I dont know how to match the string variable name with the variable value. I have put the whole code below to try and make it a little clearer. Any help would be gratefully appreciated!
)
Public Sub doActions(Actions As String, TestInstID As Integer, StateID As Integer, STID As Integer)
'do actions from the state table - only variables are function params
'declare variables to be used
Dim comma As Boolean
Dim Act1, vars1, vars2, var3, Act2 As String
Dim FromStateID, ToStateID As Integer
Dim myArray As Variant
'if STID is not null then get details from stateTransition table
If STID > 0 Then
FromStateID = DLookup("FromStateID", "StateTransition", "STID = " & STID)
ToStateID = DLookup("ToStateID", "StateTransition", "STID = " & STID)
End If
'split the string of actions into individual actions in an array
myArray = Split(Actions, "%")
'for each action swap the variable names for their values and enact the action
For Each Act1 In myArray
'replace the variables with values
'get string of variables
Char1 = InStr(1, Act1, "(")
Char2 = InStr(1, Act1, ")")
len2 = Char2 - 1 - Char1
vars1 = MID(Act1, Char1 + 1, len2)
vars2 = ""
'loop through replace each variable name with its value
Do While vars1 <> ""
'get the variable name and check if there is a comma after it
'then reduce the variable name string being checked
If InStr(1, vars1, ",") > 0 Then
var3 = Left(vars1, InStr(1, vars1, ",") - 1)
comma = True
vars1 = MID(vars1, InStr(1, vars1, ",") + 1)
Else
var3 = vars1
comma = False
vars1 = ""
End If
'get the value of the variable and add it back into the list (with a comma after it where needed)
vars2 = vars2 + CStr(Eval(var3)) 'this is what won't work!!!!
If comma = True Then
vars2 = vars2 & ","
End If
Loop
'create the new action string with the new variable list
Act2 = Left(Act1, Char1 - 1) & "(" & vars2 & ")"
'run action
Eval Act2
Next Act1
End Sub
I am trying to get the value of a variable by evaluating a string which matches the variable name.
As part of a State Machine I have a list of function names (delimited with %) that I want to run. The function string contains variable names. I want to use eval() to run the function but it cant convert the variable names in the string into their respective values from the sub that is calling eval().
To counter this I want to pull each variable out in turn, convert it to its value and then put it back in the string. The problem is I dont know how to match the string variable name with the variable value. I have put the whole code below to try and make it a little clearer. Any help would be gratefully appreciated!

Public Sub doActions(Actions As String, TestInstID As Integer, StateID As Integer, STID As Integer)
'do actions from the state table - only variables are function params
'declare variables to be used
Dim comma As Boolean
Dim Act1, vars1, vars2, var3, Act2 As String
Dim FromStateID, ToStateID As Integer
Dim myArray As Variant
'if STID is not null then get details from stateTransition table
If STID > 0 Then
FromStateID = DLookup("FromStateID", "StateTransition", "STID = " & STID)
ToStateID = DLookup("ToStateID", "StateTransition", "STID = " & STID)
End If
'split the string of actions into individual actions in an array
myArray = Split(Actions, "%")
'for each action swap the variable names for their values and enact the action
For Each Act1 In myArray
'replace the variables with values
'get string of variables
Char1 = InStr(1, Act1, "(")
Char2 = InStr(1, Act1, ")")
len2 = Char2 - 1 - Char1
vars1 = MID(Act1, Char1 + 1, len2)
vars2 = ""
'loop through replace each variable name with its value
Do While vars1 <> ""
'get the variable name and check if there is a comma after it
'then reduce the variable name string being checked
If InStr(1, vars1, ",") > 0 Then
var3 = Left(vars1, InStr(1, vars1, ",") - 1)
comma = True
vars1 = MID(vars1, InStr(1, vars1, ",") + 1)
Else
var3 = vars1
comma = False
vars1 = ""
End If
'get the value of the variable and add it back into the list (with a comma after it where needed)
vars2 = vars2 + CStr(Eval(var3)) 'this is what won't work!!!!
If comma = True Then
vars2 = vars2 & ","
End If
Loop
'create the new action string with the new variable list
Act2 = Left(Act1, Char1 - 1) & "(" & vars2 & ")"
'run action
Eval Act2
Next Act1
End Sub