ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Adding numbers in text box (https://www.excelbanter.com/excel-programming/323136-adding-numbers-text-box.html)

Doug Loewen

Adding numbers in text box
 
I have a form with 3 text boxes. I want to add the values
in each to get a total and show the total in a label. I'm
using this code:
Total = txtHouseCost.Value + txtLotCost.Value +
txtOptionCost.Value
lblTotalNum = Total
If I enter a 1, a 2 and a 3 in each box I get 123 not 6.
It looks like I am concattinating, not summing.
What have I missed?
Thanks for the help!

JulieD

Adding numbers in text box
 
Hi Doug

By default textboxes entries are treated as "text" - so you need to coerce
the results to a number

using one of the following functions
Total = CINT(txtHouseCost.Value) + CINT(txtLotCost.Value) +
CINT(txtOptionCost.Value)

or
Total = CDBL(txtHouseCost.Value) + CDBL(txtLotCost.Value) +
CDBL(txtOptionCost.Value)

or
Total = val(txtHouseCost.Value) + val(txtLotCost.Value) +
val(txtOptionCost.Value)

or even
Total = --(txtHouseCost.Value) + --(txtLotCost.Value)
+ --(txtOptionCost.Value)


Cheers
JulieD


"Doug Loewen" wrote in message
...
I have a form with 3 text boxes. I want to add the values
in each to get a total and show the total in a label. I'm
using this code:
Total = txtHouseCost.Value + txtLotCost.Value +
txtOptionCost.Value
lblTotalNum = Total
If I enter a 1, a 2 and a 3 in each box I get 123 not 6.
It looks like I am concattinating, not summing.
What have I missed?
Thanks for the help!




Bob Phillips[_6_]

Adding numbers in text box
 
Interestingly, it doesn't seem to be necessary to coerce all of the textbox
values, just one seems to do. So

CInt(txtHouseCost.Text) + txtLotCost.Text + txtOptionCost.Text

works. I don't think I would personally recommend it, I wouldn't like to
rely on this 'fetaure' always working, but it is interesting.

You can also use +0 or *1 as operators. Probably best to avoid Cint or CLng
for flexibility.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"JulieD" wrote in message
...
Hi Doug

By default textboxes entries are treated as "text" - so you need to coerce
the results to a number

using one of the following functions
Total = CINT(txtHouseCost.Value) + CINT(txtLotCost.Value) +
CINT(txtOptionCost.Value)

or
Total = CDBL(txtHouseCost.Value) + CDBL(txtLotCost.Value) +
CDBL(txtOptionCost.Value)

or
Total = val(txtHouseCost.Value) + val(txtLotCost.Value) +
val(txtOptionCost.Value)

or even
Total = --(txtHouseCost.Value) + --(txtLotCost.Value)
+ --(txtOptionCost.Value)


Cheers
JulieD


"Doug Loewen" wrote in message
...
I have a form with 3 text boxes. I want to add the values
in each to get a total and show the total in a label. I'm
using this code:
Total = txtHouseCost.Value + txtLotCost.Value +
txtOptionCost.Value
lblTotalNum = Total
If I enter a 1, a 2 and a 3 in each box I get 123 not 6.
It looks like I am concattinating, not summing.
What have I missed?
Thanks for the help!







All times are GMT +1. The time now is 05:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com