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.

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, learning, and sharing knowledge.