Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to calculate from a string like
Message
From
04/12/2001 01:56:00
 
General information
Forum:
Visual Basic
Category:
Other
Miscellaneous
Thread ID:
00588695
Message ID:
00589087
Views:
23
>My database contains a table of formulas that the users can insert into and edit. Being in a database field, they are stored as strings. How do I get VB to calculate the formula, once I read the formula string in from the database? My formulas are very complicated but using the example 'x + y', how do I get VB to calculate the value and give me the result? If I read the string into a variable like Calculation (ex. Calculation = rsDBcalc("Formula")), can I do something like Execute(Calculation) which would return the value of x + y ?
>
>For a more complicated situation, if the database Formula field = 'IF a > b THEN a - b ELSE b - a END IF', how can I do that?
>
>Much thanks for your time.
>John

You can execute simple formulas with the script control / scripting runtime like so:
Dim oSC As ScriptControl
Dim iVal As Integer
Set oSC = New ScriptControl
oSC.Language = "VBScript"

Dim toexe As String
toexe = "4*5"
iVal = oSC.Eval(toexe)

MsgBox iVal, vbOKOnly, "Value of iVal"
From your DB formulas you would have to create vars with the same names (eg x, y, etc.) and concatenate them into the string to execute. For more complex situations a little function that uses the Execute or Eval methods (and some thinking) should do the trick.
Previous
Reply
Map
View

Click here to load this message in the networking platform