-
BACKGROUND : Teaching brother vb, starting with 3 txtboxes on a form simple calculator example (4 buttons add, minus, divide, multiply). type number into txt1 & txt2, hit one of the 4 buttons, hey presto, total in box3.
PROBLEM : :confused:
OK, I use
Code:
txt3.text = txt1.text * txt2.text
which works fine with * & /. With the + & - however I get the following:
- ON the -, no entries appear
- On the +, 2 numbers join, not add (I.e.- 1+1=11, 2+2=22)
Now even I know 2+2 is not 22, this just doesn't add up (pun intended there, sorry, had to put it in). Can anyone shed some light on this please? :(
Thank you!
-
I believe the problem is that you are using strings to do math. What you should do is to put your numbers in variables ex. integer, long etc.
lars
-
Think of the Data Type
Convert the Text into a numeric data type
either Integer, Single, Double, Byte etc..
txt3.text = CInt(txt1.text) * CInt(txt2.text)
The reason for getting 22 and 11 is because it is concatenating two strings
Hope that helps
Steve
-
Convert
Convert ! And use the right operators. Don't use & to add because it will concatenate the strings.
Do something like (I didn't test this, wrote it straight into the reply):
Code:
dim dblValue1 as double
dim dblValue2 as double
if IsNumeric(txt1) then
dblValue1 = cdbl(txt1)
if IsNumeric(txt2) then
dblValue2 = cdbl(txt2)
txt3 = cstr(dblValue1 + dblValue2)
Else
msgbox "Non-numeric value
End
Else
msgbox "Non-numeric value
EndIf
Don't rely on VB to convert values properly. It will often go well, but it will go wrong when you least expect it.
-
Well A + B = AB. When you add text you're concatenatin strings. Textboxes have text in the text property (yeah, no kidding) :)
What you want to do is to convert the text into number values.
Code:
txt3.Text = Val(txt1.Text) + Val(txt2.Text)
Good luck!
-
Wow, I've never seen so many people answering a question at virtually the same time.
Just one comments though;
Don't use CInt if you're not only calculation integers.
-