Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding and subtracting 2

Status
Not open for further replies.

k7k1z1

Programmer
Sep 25, 2001
3
0
0
CA
hello, I'm VERY new to visual basic and am having problems
trying to get my .exe project to add numbers together.This is what I write.

lbltotal.Caption = txt1.Text + txt2.Text + txt3.Text
cmdjhclose.Setfocus

Though it won't actually give me the option to put ".Text"
it only gives the options to put "count", "item" , "lbound" ,
and "ubound". If anyone could give me some advice I would appriciate it, thanks.
 
Create form with three text boxes(text1,text2,text3), one CommandButton(cmdAdd) and one label(label1). Double click on the button and add the following code.

Private Sub cmdAdd_Click()

Dim x1, x2, x3, sum As Single

x1 = Val(Text1.Text)
x2 = Val(Text2.Text)
x3 = Val(Text3.Text)
sum = x1 + x2 + x3
Label1 = sum



Next buy a VB Book..
End Sub
 
From the sounds of it, you have a control array. At least that's when the bounding and item properties are present.

If your sure the names are correct remove the value from the index property in the design. You can also add the index to your code like [tt]txt1(0).Text[/tt]. Make sure the indexes in your code match up with those in your design.

Other than that, the Text property of a control is a string. With strings addition causes concatonation. To treat the control values as numbers use the VAL function as instructed above.





Wil Mead
wmead@optonline.net

 
thank you very much all info was very helpfull I used the val function and added the index in my code and everthing works fine, thanks again.
 
The VAL function only recognizes the "." as a valid decimal separator. For some international applications this can bite you. The CDbl or CSng functions avoid this pitfall. Dan Grogan
dan@siqual.com

"Absit prudentia nil rei publicae profitur."
Without common sense you ain't gonna have nothing.
 
There are other issues surrounding VAL which would deter me fromn using it. Dan is right; use explicit conversion functions instead.
 
OK thanks for the info, I'll give the other
functions a try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top