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!
Printable View
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!
Convert one of the values of the textbox to a number when adding them.
?
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.
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.
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.
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
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]
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