0

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.

Edited by mavtcr

3
Contributors
4
Replies
25
Views
3 Years
Discussion Span
Last Post by rishif2
Featured Replies
  • 1
    ddanbe 2,525   3 Years Ago

    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 Read More

  • 1
    ddanbe 2,525   3 Years Ago

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

1

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

Edited by Reverend Jim: typo

1

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

Edited by ddanbe: add

0

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))

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.