Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using "if-then" in a math statement and MS Script Control

Status
Not open for further replies.

KerryC

Technical User
Apr 9, 2002
36
CA
I'm using the MS Script Control to evaluate some simple mathematical equations like "a + b * c" but is there a way to use flow control in the same manner. What I mean is: if I have a variable, say g, in my code and it gets set equal to "on", is there a way to evaluate something like:

"if g = "on" then a + b * c else a - b * c"

I haven't been able to find anything on line that shows me if this is possible. If anyone can point to a good reference it would be appreciated, thanks.

KerryC
 
Sure

MyValue = IIF(g = "on", a + b * c, a - b * c)
 
StoolPigeon: Thanks for the link. I've done some perusing but still can't find what I'm looking for. I'll keep reading.

Golom: I wasn't clear enough in my question. The problem is that I'm reading in strings from a database. The strings are mathematical equations that use basic spreadsheet type formulas and syntax. When the strings contain more than just simple math, the Eval function doesn't seem to handle it. For example:

Dim TheResult As Double
Dim objSc As ScriptControl
Set objSc = New ScriptControl
objSc.Language = "VBScript"
TheResult = CDbl(objSc.Eval("(1+2)*4"))

will yield a value of 12 for TheResult, but:

Dim TheResult As Double
Dim g as Integer
Dim objSc As ScriptControl
Set objSc = New ScriptControl
objSc.Language = "VBScript"
g = 1
TheResult = CDbl(objSc.Eval("if g = 1 then (1+2)*4 end if"))

gives a syntax error.

I was hoping there was some reasonably simple way to evaluate these calculations.

KerryC
 


I think what Golom meant was to use the iif function outside the .Eval()

expression = IIf(g = 1, "(a + b) * c", "(a - b) * c")
TheResult = CDbl(objSc.Eval(expression))


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Mark: Yes, I'd have no problem using it outside the .Eval() but unfortunately the if-then is already in the string that I'm reading in and want to evaluate.

Thanks,

KerryC
 
Something like:

objSc.ExecuteStatement "g=1: if g = 1 then h=(1+2)*4 end if"
MsgBox objSc.Eval("h")
 
strongm: Thanks VERY much! I had nearly given up, but this works like a charm. Now I just have to find out what functions/formulas are acceptable in VBScript. It seems I'm able to use left() but not sum(). Anyway, thanks again.

KerryC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top