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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with Files !!! 1

Status
Not open for further replies.

bledileka

Programmer
Nov 15, 2006
16
Hello everyone !

Im tryin to use a formula wich is loaded from a simple file to a Label named "formula".

Formula type is like : (2 * x) + (2 * y).

In the same form i have puted 2 text boxes. One named x and the other named y. When i press the button Calculate i want to have the result using this formula.

Private Sub Calculate_Click()
result = formula
End Sub
rsult is a Label too.
The result gives me only the formula in string format the way it is without making the calculations.

Can anybody help me on this ???
 
If you have this on the Form:
Code:
 ([blue]2[/blue] * [red]x[/red]) + ([blue]2[/blue] * [red]y[/red])

Where BLUE is a label and RED is a text box, you may do:

Code:
MsgBox=(Val([blue]lblBlue.Caption[/blue]) * Val([red]txtX.Text[/red])) _
     + (Val([blue]lblBlue.Caption[/blue]) * Val([red]txtY.Text[/red]))

HTH

---- Andy
 
(2 * x) + (2 * y) is in a label
then in two text boxes named x and y i enter the values.
the label contains the whole formula. Is that possible that when i press calculate, the result takes the formula from the label and replaces x and y with entered values from textboxes ?
i uploaded an image maybe it will help better.
img.JPG

thnx.
 
And if the formula changes (as the OP suggests it will)? Perhaps to (x^3)/(2 * (x + y))...
Code:
[blue]    With CreateObject("msscriptcontrol.scriptcontrol")
        .Language = "vbscript"
        
        .AddCode "x =" & txtX
        .AddCode "y = " & txtY
        
        lblResult = .Eval(lblFormula)
    End With[/blue]

 
You can do this.

1. Start a new project
2. Click Project -> Refernences
3. Select 'Microsoft Script Control 1.0'

4. Create a form with 1 label control, 2 text boxes, and a command button.
5. Add this code to the form.

Code:
Option Explicit

Private Sub Command1_Click()
    
    Dim oScript As ScriptControl
    Dim cTemp As String
    
    cTemp = Label1.Caption
    cTemp = Replace(cTemp, "x", Text1.Text)
    cTemp = Replace(cTemp, "y", Text2.Text)
    
    
    Set oScript = New ScriptControl
    oScript.Language = "VBScript"
    
    MsgBox oScript.Eval(cTemp)
    
End Sub

Private Sub Form_Load()
    
    Label1.Caption = "(2 * x) + (2 * y)"
    Text1.Text = "4"
    Text2.Text = "5"
    
End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top