Reputation: 13
I wanted to make a summation of two values from two different text boxes in a form so I had typed the codes correctly as shown:
Private Sub btnResult_Click()
Dim answer As Integer
answer = txtNum1 + txtNum2
txtAns.Value = answer
End Sub
That code should output in the box that named txtAns
the sum of txtNum1 + txtNum2
when I click on the button btnResult
, But instead it output the two values next to each other without any calculation as shown in this picture:
Click here to open the picture!
But, when I use another operator like ( / or * or - ) the calculation's output correctly!
So, What's going on?
Upvotes: 0
Views: 5184
Reputation: 25266
In the following example:
Dim answer As Integer
answer = "1" + "2"
Debug.Print answer
the integer value 12
is printed. What happens is that first the text expression "1" + "2"
is evaluated, which yields the string "12"
, which is the converted to integer value 12
.
If you would do instead:
answer = Int("1") + Int("2")
the value 3
will be printed.
In your script you can do:
answer = Int(txtNum1.Value) + Int(txtNum2.Value)
The .Value
qualifier is probably not necessary as it is the default attribute.
Upvotes: 1