# calculate formula in textbox

• 11 years ago

Hi, I need help with creating a function to calculate a formula in a textbox.

eg.

textbox1.text  = "[input1] -3 / 4"     'where input1 is data stored in a database

output1 = calculate(textbox1.text)

Any help would be greatfully appreciated.

• 11 years ago

You can create your own Formula Evaluation function which needs lots of work. If you want to work it out quickly you can use Microsoft Script Control.

output1 = ScriptControl1.Eval(textbox1.text)

• 11 years ago
novavb6 wrote:

Hi, I need help with creating a function to calculate a formula in a textbox.

eg.

textbox1.text  = "[input1] -3 / 4"     'where input1 is data stored in a database

output1 = calculate(textbox1.text)

Any help would be greatfully appreciated.

[Code]
Private Function Calculate(ByVal input1 as Integer) as Integer
'You can change Parameter type according to your need like "Integer" to Long,Single,Double.....'
Calculate=input1-3/4
End Function
[/Code]

To call the Function,

[Code]
Private Sub Command1_Click()
Dim mycalculationresult as Integer
'Retrive record from Database which is to be sent as input parameter to the function
mycalculationresult=Calculate(recordsetname.fieldname)
End Sub

[/Code]

• 11 years ago

thanks guys, I will try these and see how it goes.

• 11 years ago

thanks, I used the scriptcontrol and it worked well.

The input data is not actually stored in the database, just the caption to prompt for data

I first replaced the text using replace. anyway here is some code

Private Sub cmdCalculate_Click()
y = 0
z = 0
For y = 1 To txtOutputFormula.Count - 1
For z = 1 To Text1.Count - 1
'startpos = InStr(1, txtOutputFormula(y).Text, "[" & Text1(z).Tag & "]")
'If startpos > 0 Then

tempstr = Replace(txtOutputFormula(y).Text, "[" & Text1(z).Tag & "]", Text1(z).Text)
MsgBox tempstr
txtOutputFormula(y).Text = tempstr
'End If

'MsgBox "[" & Text1(z).Tag & "]"

Next z
Next y

For p = 1 To txtResult.Count - 1
txtResult(p).Text = ScriptControl1.Eval(txtOutputFormula(p))
txtResult(p).Visible = True

Next p

End Sub

### Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

### Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone”