Friends,
Please help me

I have a form having 3 text boxes of data type number one text box for display- also number type
one command button
I want to add the figures of the three text boxes
my code is as below

Private Sub Command1_Click()
Dim Total As Long
Total = (Text1.Text + Text2.Text + Text3.Text)
Text4.Text = Total
End Sub

When I entered 1 in the first text box, 2 in the second and 3 in the third,on clicking the button
I am getting the answer 123 instead of 6
Why this anomaly? please help me.

Dani AI

Generated

Short version: the TextBox properties you used are strings, so the UI was concatenating "1" + "2" + "3" into "123" instead of doing numeric addition. correctly pointed to that root cause and offered conversion approaches — below are a few practical, safer ways to handle this (for both classic VB and VB.NET), plus a couple of gotchas.

In Visual Basic the safest pattern is: validate each Text value, convert to an appropriate numeric type, do the arithmetic, then convert the result back to text for display. Don’t rely on implicit conversions (they can behave differently with + vs & and across VB versions), and reject or default non-numeric input. Watch decimal separators (culture), and use a wider type (Long or Double) if overflow is possible.

Example (VB.NET — uses TryParse so invalid input becomes 0 without exception):

' VB.NET
Dim a As Integer = 0
Dim b As Integer = 0
Dim c As Integer = 0
Integer.TryParse(TextBox1.Text.Trim(), a)
Integer.TryParse(TextBox2.Text.Trim(), b)
Integer.TryParse(TextBox3.Text.Trim(), c)
TextBox4.Text = (a + b + c).ToString()

Example (VB6/classic VB — validate with IsNumeric before converting):

' VB6
Dim n1 As Long, n2 As Long, n3 As Long, total As Long
If IsNumeric(Trim$(Text1.Text)) Then n1 = CLng(Trim$(Text1.Text)) Else n1 = 0
If IsNumeric(Trim$(Text2.Text)) Then n2 = CLng(Trim$(Text2.Text)) Else n2 = 0
If IsNumeric(Trim$(Text3.Text)) Then n3 = CLng(Trim$(Text3.Text)) Else n3 = 0
total = n1 + n2 + n3
Text4.Text = CStr(total)

Extra tips: use a numeric control (NumericUpDown / spin control) where possible to avoid parsing; prefer CDbl/CLng for the required precision; and use explicit ToString/CStr when assigning back to a Text property. This keeps behavior consistent and avoids the "123 vs 6" surprise that started the thread.

Recommended Answers

All 4 Replies

You are concatenating the strings in the 3 textboxes. First convert the Text strings to numbers, add, and convert the result to a string again to feed Text4

Will you modify the code for me please

Use the Val function.
Example Num1 = Val(Text1.Text) etc.

i am 100% agree with the solution of ddanbe.
but you can also get the same result with this:-

Dim x As Integer, y As Integer, z As Integer
x = Text1.Text
y = Text2.Text
z = Text3.Text
Text4.Text = x + y + z

but ,here i recommend to use val(as ddanbe recommend also it) because creating new variable consume memory and here it is not necessary as we can also get the same result without using variables.

so this is better than the above code

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.