Results 1 to 8 of 8

Thread: Adding values from textboxes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 1999
    Posts
    153
    I'm able to subtract the values of two textboxes, however, when I go to add the values of the two textboxes using the "+" it's concatenated as a string running the two values together. How do I get around this issue.

    Thanks!

  2. #2
    Lively Member
    Join Date
    Jan 2000
    Location
    Springfield, IL
    Posts
    124
    Convert one of the values of the textbox to a number when adding them.

  3. #3
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    ?

    If you're putting the results into another TextBox or into a String then it will concatenate. Either assign the results to a numeric variable and then assign that variable to the TextBox <or> use the Val on the TextBox values, for example:

    Text1.Text = Val(Text1.Text) + Val(Text2.Text)

    Of course with this method you'd have to add your own error checking (the text is actually numeric, decimals, etc). Also try looking into the C___ conversion functions if you need a specific data-type, some of these are CInt, CSng, CDbl, and Clng.

  4. #4
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Correct a "+" is use for arithmetic and a "&" is use for concatenation in VB.

    Your values are in string format and must be convert to a numeric. You can use "Cint" or something similar to that.
    Chemically Formulated As:
    Dr. Nitro

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 1999
    Posts
    153

    Adding textbox values

    Thanks guys. I tried the suggestions above and was able to return a value. However, the return value is truncated, whereas, it would add the outer most numbers to the left of the comma. Ex: 671,435 + -16,443 returns 655 (671-16). For the interim, I was able to get the correct number by using two "-" back to back. It works for now, but afraid it may catch up with me.

  6. #6
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    Hi, there.

    Try this code:

    Dim str As String

    str = CLng(Text1) + CLng(Text2)
    Text3 = Format(str, "###,###")

    You need to use Format function to insert coma in your result (654,992), otherwise you'll get a result without coma (654992).

    Larisa

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 1999
    Posts
    153

    Adding Textbox values

    Thanks Larissa. It works. You wouldn't know a quicker way of doing multiple textboxes. Maybe using an array or something of the sort.

    [Edited by Hutty on 04-06-2000 at 09:02 AM]

  8. #8
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Dim lng_Result As Long
    Dim NitroText As Control

    For Each NitroText In Form1.Controls
    If TypeOf NitroText Is TextBox Then
    'PURPOSE: Check if data is numeric or string
    If IsNumeric(NitroText.Text) Then
    lng_Result = lng_Result + NitroText.Text
    Else
    MsgBox "Data in " & """" & NitroText.Name & """" & " is not a numeric!."
    End If
    End If
    Next
    MsgBox lng_Result
    Chemically Formulated As:
    Dr. Nitro

Posting Permissions

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



Click Here to Expand Forum to Full Width