extracting value of formula stored in a text box

Hi

I am writing a program in VB6.0 and I cant seem to get around this. Please help if any one can.
The problem is

The user enters a formula in a text box, now how do i extract the value derived from this formula.

example : the user wants to see 33% of the BASIC SALARY OF EMPLOYEES

text1="BASIC*(3/100)"

now how do i extract the value based on the formula. (BASIC is a COLUMN/FIELD in a TABLE"

Thanks in advance
«1

Comments

  • Write your own formula parser.
    With VB automation you can parse the constants and the operator, but for the "BASIC" field you have to write you own procedures to read from the correct database, table and field.
    Then you input the value of the field into the formula and then process it with VB automation.

    ----------------

    : Hi
    :
    : I am writing a program in VB6.0 and I cant seem to get around this. Please help if any one can.
    : The problem is
    :
    : The user enters a formula in a text box, now how do i extract the value derived from this formula.
    :
    : example : the user wants to see 33% of the BASIC SALARY OF EMPLOYEES
    :
    : text1="BASIC*(3/100)"
    :
    : now how do i extract the value based on the formula. (BASIC is a COLUMN/FIELD in a TABLE"
    :
    : Thanks in advance
    :

    [red]Good luck![/red]
    [blue]Hackman[/blue]

  • Hi Hackman

    Thanks for the suggestion. I have already rephrased the text and added the appropriate recordset references. "(RST!BASIC*(33/100))". The problem is how do i extract the values referenced by these variables.

    Like VAL("RST!BASIC") is bound to RETURN 0.. so how ?

    hopeing for a solution

    Thanks again

    AWAHI

    : Write your own formula parser.
    : With VB automation you can parse the constants and the operator, but for the "BASIC" field you have to write you own procedures to read from the correct database, table and field.
    : Then you input the value of the field into the formula and then process it with VB automation.
    :
    : ----------------
    :
    : : Hi
    : :
    : : I am writing a program in VB6.0 and I cant seem to get around this. Please help if any one can.
    : : The problem is
    : :
    : : The user enters a formula in a text box, now how do i extract the value derived from this formula.
    : :
    : : example : the user wants to see 33% of the BASIC SALARY OF EMPLOYEES
    : :
    : : text1="BASIC*(3/100)"
    : :
    : : now how do i extract the value based on the formula. (BASIC is a COLUMN/FIELD in a TABLE"
    : :
    : : Thanks in advance
    : :
    :
    : [red]Good luck![/red]
    : [blue]Hackman[/blue]
    :
    :

  • [b][red]This message was edited by DrMarten at 2006-5-12 13:32:37[/red][/b][hr]

    Hi,

    You could put the whole text into a string.

    Search through the string for keywords.

    Search through a string till the character changes from a number to something else to extract each number.

    Or ask the user to enter the equation terms separated by spaces.
    Then.>>

    [code]
    Dim equationTerms() As String
    equationTerms=Split(Textbox1) 'Assuming textbox1 is for input.
    [/code]
    Then process each item of the array elements yourself with a series of IF THEN statements.

    Don't forget>>
    [code]
    Dim uLimit As Integer
    uLimit=Ubound(equationTerms) 'Returns the last Array
    '-element value or it's upperBound.
    [/code]

    Alternatively create a small calculator type program using various buttons for your functions.

    you could have one textbox for the result and one or more textboxes for numeric ( number ) input.

    Dont forget to process like BODMAS though.
    Brackets first then ^ as in 2^3
    Divide
    Multiply
    Add
    Subtract

    For your sums to be right.
    Any decent calculator will say 2+2*2=6 (Multiply is 1st).
    These are rules from PURE maths too as a sum or equation can only have one answer ( unless it is a simultaneous equation).


    Regards,

    Dr M.

  • Hi Dr M

    Thanks for the suggestion.

    I have already worked on the BODMS principle and have successfully broken up the formula.

    The problem still remains with the memory variable and/or field refrence that are stored in as a string. I want to reference the value of the variable name stored in the string.

    Something like pointers in C or the macro in Clipper/FoxPro &(VAR) (where VAR="f->BASIC or VAR=m->BASIC)

    The CALCULATOR option as suggested by you is a great idea, but, this option will also required by me in some of the procesing to be done where the formula is defined by the user & stores in a database. so direct string conversion will be the only option considerable in the present context.

    I am sure there is a way around this in VB.

    Hopeing for more suggestions

    Thanks & ragards

    AWAHI


    : [b][red]This message was edited by DrMarten at 2006-5-12 13:32:37[/red][/b][hr]
    :
    : Hi,
    :
    : You could put the whole text into a string.
    :
    : Search through the string for keywords.
    :
    : Search through a string till the character changes from a number to something else to extract each number.
    :
    : Or ask the user to enter the equation terms separated by spaces.
    : Then.>>
    :
    : [code]
    : Dim equationTerms() As String
    : equationTerms=Split(Textbox1) 'Assuming textbox1 is for input.
    : [/code]
    : Then process each item of the array elements yourself with a series of IF THEN statements.
    :
    : Don't forget>>
    : [code]
    : Dim uLimit As Integer
    : uLimit=Ubound(equationTerms) 'Returns the last Array
    : '-element value or it's upperBound.
    : [/code]
    :
    : Alternatively create a small calculator type program using various buttons for your functions.
    :
    : you could have one textbox for the result and one or more textboxes for numeric ( number ) input.
    :
    : Dont forget to process like BODMAS though.
    : Brackets first then ^ as in 2^3
    : Divide
    : Multiply
    : Add
    : Subtract
    :
    : For your sums to be right.
    : Any decent calculator will say 2+2*2=6 (Multiply is 1st).
    : These are rules from PURE maths too as a sum or equation can only have one answer ( unless it is a simultaneous equation).
    :
    :
    : Regards,
    :
    : Dr M.
    :
    :

  • [b][red]This message was edited by DrMarten at 2006-5-13 2:59:40[/red][/b][hr]
    : Hi Dr M
    :
    : Thanks for the suggestion.
    :
    : I have already worked on the BODMS principle and have successfully broken up the formula.
    :
    : The problem still remains with the memory variable and/or field refrence that are stored in as a string. I want to reference the value of the variable name stored in the string.
    :
    : Something like pointers in C or the macro in Clipper/FoxPro &(VAR) (where VAR="f->BASIC or VAR=m->BASIC)
    :
    : The CALCULATOR option as suggested by you is a great idea, but, this option will also required by me in some of the procesing to be done where the formula is defined by the user & stores in a database. so direct string conversion will be the only option considerable in the present context.
    :
    : I am sure there is a way around this in VB.
    :
    : Hopeing for more suggestions
    :
    : Thanks & ragards
    :
    : AWAHI
    :
    :
    : : [b][red]This message was edited by DrMarten at 2006-5-12 13:32:37[/red][/b][hr]
    : :
    : : Hi,
    : :
    : : You could put the whole text into a string.
    : :
    : : Search through the string for keywords.
    : :
    : : Search through a string till the character changes from a number to something else to extract each number.
    : :
    : : Or ask the user to enter the equation terms separated by spaces.
    : : Then.>>
    : :
    : : [code]
    : : Dim equationTerms() As String
    : : equationTerms=Split(Textbox1) 'Assuming textbox1 is for input.
    : : [/code]
    : : Then process each item of the array elements yourself with a series of IF THEN statements.
    : :
    : : Don't forget>>
    : : [code]
    : : Dim uLimit As Integer
    : : uLimit=Ubound(equationTerms) 'Returns the last Array
    : : '-element value or it's upperBound.
    : : [/code]
    : :
    : : Alternatively create a small calculator type program using various buttons for your functions.
    : :
    : : you could have one textbox for the result and one or more textboxes for numeric ( number ) input.
    : :
    : : Dont forget to process like BODMAS though.
    : : Brackets first then ^ as in 2^3
    : : Divide
    : : Multiply
    : : Add
    : : Subtract
    : :
    : : For your sums to be right.
    : : Any decent calculator will say 2+2*2=6 (Multiply is 1st).
    : : These are rules from PURE maths too as a sum or equation can only have one answer ( unless it is a simultaneous equation).
    : :
    : :
    : : Regards,
    : :
    : : Dr M.

    ======================================================================
    Hi,

    CObj Example
    This example uses the CObj function to convert a numeric value to Object. The Object variable itself contains only a four-byte pointer, which points to the Double value assigned to it.
    [code]
    Dim MyDouble As Double
    Dim MyObject As Object
    MyDouble = 2.7182818284
    MyObject = CObj(MyDouble) ' Double value is pointed to by MyObject.
    [/code]

    But you would use
    [code]
    Dim myString ,newString As String
    Dim myObject As Object
    myString="*"
    myObject=CObj(myString)
    'I'm guessing here but i believe myObject would point to a value of "*"
    ' in the same way that newString="*"
    'so you could then say [b][blue]IF myObject="*" THEN 'do your
    'MULTIPLY routine.[/b][/blue]
    [/code]


    Regards,

    Dr M.



  • : Hi
    :
    : I am writing a program in VB6.0 and I cant seem to get around this. Please help if any one can.
    : The problem is
    :
    : The user enters a formula in a text box, now how do i extract the value derived from this formula.
    :
    : example : the user wants to see 33% of the BASIC SALARY OF EMPLOYEES
    :
    : text1="BASIC*(3/100)"
    :
    : now how do i extract the value based on the formula. (BASIC is a COLUMN/FIELD in a TABLE"
    :
    : Thanks in advance
    :

    [code]
    Dim SplitString() as string

    'Somewhere in a function/sub

    SplitString=Split(text1,"*")

    SQLString="SELECT (" & SplitString(0) & "*" & CDbl(Splitstring(1)) & ") AS Salary FROM Employee"

    'Execute SQL
    [/code]

    You can use the Split-Function, to split up the Text in your Textbox, i this case taking the Asteriks as the delimiter. You could write a lookup-array for the operators ("+","-","/","*") to determine, which operator is used in the string, and then split the string.

    Zvoni
    ------------------------------------------
    Only stupidity of mankind and the universe
    are infinite, but i'm not sure concerning
    the universe. A. Einstein

  • Hi Dr. M

    Thanks again. but this time while testing th COBJ sample as mentioned by yoy. i get "Compiler Error : Sub or Function not defined" on CObj(MyString)

    any suggestions ?

    Regards

    AWAHI

    : [b][red]This message was edited by DrMarten at 2006-5-13 2:59:40[/red][/b][hr]
    : : Hi Dr M
    : :
    : : Thanks for the suggestion.
    : :
    : : I have already worked on the BODMS principle and have successfully broken up the formula.
    : :
    : : The problem still remains with the memory variable and/or field refrence that are stored in as a string. I want to reference the value of the variable name stored in the string.
    : :
    : : Something like pointers in C or the macro in Clipper/FoxPro &(VAR) (where VAR="f->BASIC or VAR=m->BASIC)
    : :
    : : The CALCULATOR option as suggested by you is a great idea, but, this option will also required by me in some of the procesing to be done where the formula is defined by the user & stores in a database. so direct string conversion will be the only option considerable in the present context.
    : :
    : : I am sure there is a way around this in VB.
    : :
    : : Hopeing for more suggestions
    : :
    : : Thanks & ragards
    : :
    : : AWAHI
    : :
    : :
    : : : [b][red]This message was edited by DrMarten at 2006-5-12 13:32:37[/red][/b][hr]
    : : :
    : : : Hi,
    : : :
    : : : You could put the whole text into a string.
    : : :
    : : : Search through the string for keywords.
    : : :
    : : : Search through a string till the character changes from a number to something else to extract each number.
    : : :
    : : : Or ask the user to enter the equation terms separated by spaces.
    : : : Then.>>
    : : :
    : : : [code]
    : : : Dim equationTerms() As String
    : : : equationTerms=Split(Textbox1) 'Assuming textbox1 is for input.
    : : : [/code]
    : : : Then process each item of the array elements yourself with a series of IF THEN statements.
    : : :
    : : : Don't forget>>
    : : : [code]
    : : : Dim uLimit As Integer
    : : : uLimit=Ubound(equationTerms) 'Returns the last Array
    : : : '-element value or it's upperBound.
    : : : [/code]
    : : :
    : : : Alternatively create a small calculator type program using various buttons for your functions.
    : : :
    : : : you could have one textbox for the result and one or more textboxes for numeric ( number ) input.
    : : :
    : : : Dont forget to process like BODMAS though.
    : : : Brackets first then ^ as in 2^3
    : : : Divide
    : : : Multiply
    : : : Add
    : : : Subtract
    : : :
    : : : For your sums to be right.
    : : : Any decent calculator will say 2+2*2=6 (Multiply is 1st).
    : : : These are rules from PURE maths too as a sum or equation can only have one answer ( unless it is a simultaneous equation).
    : : :
    : : :
    : : : Regards,
    : : :
    : : : Dr M.
    :
    : ======================================================================
    : Hi,
    :
    : CObj Example
    : This example uses the CObj function to convert a numeric value to Object. The Object variable itself contains only a four-byte pointer, which points to the Double value assigned to it.
    : [code]
    : Dim MyDouble As Double
    : Dim MyObject As Object
    : MyDouble = 2.7182818284
    : MyObject = CObj(MyDouble) ' Double value is pointed to by MyObject.
    : [/code]
    :
    : But you would use
    : [code]
    : Dim myString ,newString As String
    : Dim myObject As Object
    : myString="*"
    : myObject=CObj(myString)
    : 'I'm guessing here but i believe myObject would point to a value of "*"
    : ' in the same way that newString="*"
    : 'so you could then say [b][blue]IF myObject="*" THEN 'do your
    : 'MULTIPLY routine.[/b][/blue]
    : [/code]
    :
    :
    : Regards,
    :
    : Dr M.
    :
    :
    :
    :

  • Hi BarKeeper

    Thanks for the suggestion. but i get an error of type mismatch on CDbl

    BASIC=1000
    mVAR = "BASIC"
    mVAL = CDbl(mVAR)

    Please suggest

    Regards

    AWAHI


    : : Hi
    : :
    : : I am writing a program in VB6.0 and I cant seem to get around this. Please help if any one can.
    : : The problem is
    : :
    : : The user enters a formula in a text box, now how do i extract the value derived from this formula.
    : :
    : : example : the user wants to see 33% of the BASIC SALARY OF EMPLOYEES
    : :
    : : text1="BASIC*(3/100)"
    : :
    : : now how do i extract the value based on the formula. (BASIC is a COLUMN/FIELD in a TABLE"
    : :
    : : Thanks in advance
    : :
    :
    : [code]
    : Dim SplitString() as string
    :
    : 'Somewhere in a function/sub
    :
    : SplitString=Split(text1,"*")
    :
    : SQLString="SELECT (" & SplitString(0) & "*" & CDbl(Splitstring(1)) & ") AS Salary FROM Employee"
    :
    : 'Execute SQL
    : [/code]
    :
    : You can use the Split-Function, to split up the Text in your Textbox, i this case taking the Asteriks as the delimiter. You could write a lookup-array for the operators ("+","-","/","*") to determine, which operator is used in the string, and then split the string.
    :
    : Zvoni
    : ------------------------------------------
    : Only stupidity of mankind and the universe
    : are infinite, but i'm not sure concerning
    : the universe. A. Einstein
    :
    :

  • wrong way!

    "BASIC" is the Name of a column/Field in your DB-Table!

    Like my Example below:
    [code]
    Dim mVar as String
    dim mVal as Double
    Dim SplitString() as string

    'Somewhere in a function/sub

    SplitString=Split(text1,"*")

    mVar=SplitString(0) '--> Contains "BASIC"
    mVal=CDbl(SplitString(1)) '--> Contains "3/100" --> mVal contains then 0.3

    SQLString="SELECT (" & mVar & "*" & mVal & ") AS Salary FROM Employee"

    'Execute SQL
    [/code]


    Zvoni

    : Hi BarKeeper
    :
    : Thanks for the suggestion. but i get an error of type mismatch on CDbl
    :
    : BASIC=1000
    : mVAR = "BASIC"
    : mVAL = CDbl(mVAR)
    :
    : Please suggest
    :
    : Regards
    :
    : AWAHI
    :
    :
    : : : Hi
    : : :
    : : : I am writing a program in VB6.0 and I cant seem to get around this. Please help if any one can.
    : : : The problem is
    : : :
    : : : The user enters a formula in a text box, now how do i extract the value derived from this formula.
    : : :
    : : : example : the user wants to see 33% of the BASIC SALARY OF EMPLOYEES
    : : :
    : : : text1="BASIC*(3/100)"
    : : :
    : : : now how do i extract the value based on the formula. (BASIC is a COLUMN/FIELD in a TABLE"
    : : :
    : : : Thanks in advance
    : : :
    : :
    : : [code]
    : : Dim SplitString() as string
    : :
    : : 'Somewhere in a function/sub
    : :
    : : SplitString=Split(text1,"*")
    : :
    : : SQLString="SELECT (" & SplitString(0) & "*" & CDbl(Splitstring(1)) & ") AS Salary FROM Employee"
    : :
    : : 'Execute SQL
    : : [/code]
    : :
    : : You can use the Split-Function, to split up the Text in your Textbox, i this case taking the Asteriks as the delimiter. You could write a lookup-array for the operators ("+","-","/","*") to determine, which operator is used in the string, and then split the string.
    : :
    : : Zvoni
    : : ------------------------------------------
    : : Only stupidity of mankind and the universe
    : : are infinite, but i'm not sure concerning
    : : the universe. A. Einstein
    : :
    : :
    :
    :

    ------------------------------------------
    Only stupidity of mankind and the universe
    are infinite, but i'm not sure concerning
    the universe. A. Einstein

  • That's because you have to use the SET-Statement when assigning a variable to an object

    Set MyObject=CObj(MyString)

    Zvoni

    P.S.: IMHO this way it's too overboard.
    Initially, your problem is just a Parser-Code-Issue, nothing else. And Parser-Code is just String-Manipulation.

    : Hi Dr. M
    :
    : Thanks again. but this time while testing th COBJ sample as mentioned by yoy. i get "Compiler Error : Sub or Function not defined" on CObj(MyString)
    :
    : any suggestions ?
    :
    : Regards
    :
    : AWAHI
    :
    : : : ======================================================================
    : : Hi,
    : :
    : : CObj Example
    : : This example uses the CObj function to convert a numeric value to Object. The Object variable itself contains only a four-byte pointer, which points to the Double value assigned to it.
    : : [code]
    : : Dim MyDouble As Double
    : : Dim MyObject As Object
    : : MyDouble = 2.7182818284
    : : MyObject = CObj(MyDouble) ' Double value is pointed to by MyObject.
    : : [/code]
    : :
    : : But you would use
    : : [code]
    : : Dim myString ,newString As String
    : : Dim myObject As Object
    : : myString="*"
    : : myObject=CObj(myString)
    : : 'I'm guessing here but i believe myObject would point to a value of "*"
    : : ' in the same way that newString="*"
    : : 'so you could then say [b][blue]IF myObject="*" THEN 'do your
    : : 'MULTIPLY routine.[/b][/blue]
    : : [/code]
    : :
    : :
    : : Regards,
    : :
    : : Dr M.
    : :
    : :
    : :
    : :
    :
    :

    ------------------------------------------
    Only stupidity of mankind and the universe
    are infinite, but i'm not sure concerning
    the universe. A. Einstein

  • : wrong way!
    :
    : "BASIC" is the Name of a column/Field in your DB-Table!
    :
    : Like my Example below:
    : [code]
    : Dim mVar as String
    : dim mVal as Double
    : Dim SplitString() as string
    :
    : 'Somewhere in a function/sub
    :
    : SplitString=Split(text1,"*")
    :
    : mVar=SplitString(0) '--> Contains "BASIC"
    : mVal=CDbl(SplitString(1)) '--> Contains "3/100" --> mVal contains then 0.3
    :
    : SQLString="SELECT (" & mVar & "*" & mVal & ") AS Salary FROM Employee"
    :
    : 'Execute SQL
    : [/code]
    Why do we need to Split value of TextBox?
    How about this
    [code]
    [green]' if Text1="BASIC*(3/100)" [/green]
    SQLString="SELECT (" & Text1 & ") AS Salary FROM Employee"
    [/code]
  • : : wrong way!
    : :
    : : "BASIC" is the Name of a column/Field in your DB-Table!
    : :
    : : Like my Example below:
    : : [code]
    : : Dim mVar as String
    : : dim mVal as Double
    : : Dim SplitString() as string
    : :
    : : 'Somewhere in a function/sub
    : :
    : : SplitString=Split(text1,"*")
    : :
    : : mVar=SplitString(0) '--> Contains "BASIC"
    : : mVal=CDbl(SplitString(1)) '--> Contains "3/100" --> mVal contains then 0.3
    : :
    : : SQLString="SELECT (" & mVar & "*" & mVal & ") AS Salary FROM Employee"
    : :
    : : 'Execute SQL
    : : [/code]
    : Why do we need to Split value of TextBox?
    : How about this
    : [code]
    : [green]' if Text1="BASIC*(3/100)" [/green]
    : SQLString="SELECT (" & Text1 & ") AS Salary FROM Employee"
    : [/code]
    :

    Because the SQL-Parser would interpret the complete content of the Text1-Box as a Column-Name! And there is no Column named "BASIC*3/100" in his Database

    ------------------------------------------
    Only stupidity of mankind and the universe
    are infinite, but i'm not sure concerning
    the universe. A. Einstein

  • :
    : Because the SQL-Parser would interpret the complete content of the Text1-Box as a Column-Name! And there is no Column named "BASIC*3/100" in his Database
    :
    : ------------------------------------------
    : Only stupidity of mankind and the universe
    : are infinite, but i'm not sure concerning
    : the universe. A. Einstein
    :
    :
    [b]Regret to say that[/b] but your code doesn't work

    Type mismatch Error appeared here
    [code]
    mVal=CDbl(SplitString(1))
    [/code]
    It's happened because you cann't convert string "3/100" to Double
    At the same time code
    [code]
    SQLString="SELECT " & Text1 & " AS Salary FROM Employee"
    [/code]
    returns ligimitate SQL statement (at least for MS SQL Sever)
    [code]
    SELECT BASIC*3/100 AS Salary FROM Employee
    [/code]
  • : :
    : : Because the SQL-Parser would interpret the complete content of the Text1-Box as a Column-Name! And there is no Column named "BASIC*3/100" in his Database
    : :
    : : ------------------------------------------
    : : Only stupidity of mankind and the universe
    : : are infinite, but i'm not sure concerning
    : : the universe. A. Einstein
    : :
    : :
    : [b]Regret to say that[/b] but your code doesn't work
    :
    : Type mismatch Error appeared here
    : [code]
    : mVal=CDbl(SplitString(1))
    : [/code]
    : It's happened because you cann't convert string "3/100" to Double

    [blue]
    Arrrgghh. You're right. Haven't noticed that one! There's an error because to use CDbl you would first have to execute the division! And since it's a string....grml

    [code]
    Dim sDummy() as string

    sDummy=Split(SplitString(1),"/") 'maybe a loop to look up every possible Operator?

    mVal=sDummy(0)/sDummy(1) 'AND FINALLY I HAVE MY DOUBLE-VALUE!
    [/code]
    [/blue]

    : At the same time code
    : [code]
    : SQLString="SELECT " & Text1 & " AS Salary FROM Employee"
    : [/code]
    : returns ligimitate SQL statement (at least for MS SQL Sever)
    : [code]
    : SELECT BASIC*3/100 AS Salary FROM Employee
    : [/code]
    :

    Well, if it works, who wants to complain? Anyway, trying the ODBC-API "SQLNative" (don't remember the exact name of the API-Function) is not so bad either, since this Function tests a SQL-Statement for Errors based on the Database used (SQL-Dialect) without actually executing the SQL-Statement! Damn useful Function!

    Zvoni
    ------------------------------------------
    Only stupidity of mankind and the universe
    are infinite, but i'm not sure concerning
    the universe. A. Einstein

  • : Well, if it works, who wants to complain? Anyway, trying the ODBC-API "SQLNative" (don't remember the exact name of the API-Function) is not so bad either, since this Function tests a SQL-Statement for Errors based on the Database used (SQL-Dialect) without actually executing the SQL-Statement! Damn useful Function!
    :
    I have never used those APIs. Honestly speaking, I forgot when used ODBC last time. Last 8 years, I work mostly with ADO and ADO.NET and OLE DB providers. IMHO, ODBC is yestreday of programming.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories