+ Reply to Thread
Results 1 to 3 of 3

Adding numbers in text box

  1. #1
    Doug Loewen
    Guest

    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!

  2. #2
    JulieD
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    >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!




  3. #3
    Bob Phillips
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > 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" <[email protected]> wrote in message
    > news:[email protected]...
    > >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!

    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1