PDA

View Full Version : Sum text box values



lifeson
03-05-2007, 03:20 AM
This seems so simple I'm almost embarressed to ask :confused4 but:
I have a form where I want a text box to display the total of the textbox values on the form.

Private Sub Calculate()
On Error Resume Next
TextBox3.Value = TextBox1.Value + TextBox2.Value
' TextBox3.Value = (Format(TextBox, "0.00"))
End Sub
Private Sub TextBox1_Change()
OnlyNumbers
Calculate
End Sub
Private Sub TextBox2_Change()
OnlyNumbers
Calculate
End Sub

Private Sub OnlyNumbers()
If TypeName(Me.ActiveControl) = "TextBox" Then
With Me.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Then
MsgBox "Sorry, only numbers allowed"
.Value = vbNullString
End If
End With
End If

End Sub
This works when multiplying the values
TextBox3.Value = TextBox1.Value * TextBox2.Value
So i must be in the right area???

As an add on I would like the total to be rounded to be the sum of textbox1 and textbox2, divided by 1000 and dsiplayed to 2 figures

eg

1234 + 3456 = 4.69

Bob Phillips
03-05-2007, 03:38 AM
TextBox3.Value = Format((CDbll(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00")

lifeson
03-05-2007, 04:04 AM
XLD
If I just copy your code:
TextBox3.Value = Format((CDbll(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00")

over my line
TextBox3.Value = TextBox1.Value + TextBox2.Value
I get an error:

"Sub or function not defined."

moa
03-05-2007, 04:12 AM
Look at the first CDbl function call (two "l"s). It's a typo.

Bob Phillips
03-05-2007, 04:23 AM
XLD
If I just copy your code:
TextBox3.Value = Format((CDbll(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00")
over my line
TextBox3.Value = TextBox1.Value + TextBox2.Value I get an error:

"Sub or function not defined."

Sorry, I changed the Val to CDbl just before posting (Because of an issue with another post a few days ago), but messed it. It should be

TextBox3.Value = Format((CDbl(TextBox1.Value) + CDbl(TextBox2.Value)) / 1000, "#,##0.00")

lifeson
03-05-2007, 09:28 AM
Cheers
As usual thanx for the help