Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

How to add two TextBoxes arithmetically? 1

Status
Not open for further replies.

bdickens

Mechanical
Sep 19, 2005
13
Hi all,

I have just started learning VBA.

I have a small problem that I hope somebody may be able to help me out.

I was wondering how you add two TextBoxes together arithmetically.

My code is like this:

TextBox4.Text = TextBox3.Text + TextBox2.Text

but when I put in the data i.e 149 + 92 instead of adding it up to an answer of 241 it comes out 14992.

is there a way of adding up numerically.

thanks

Barry.
 
Replies continue below

Recommended for you

Use the Val function

Good Luck
johnwm
________________________________________________________
To get the best from these forums read faq731-376 before posting

Steam Engine enthusiasts:
 
Hi,

Do you mean like

TextBox4.Value = TextBox3.Value + TextBox2.Value

I tried this before hand and it returned the same result.

Thanks

Barry

 
If you use + on strings they are appended as you have found

You want
TextBox4 = Val(TextBox3) Val(TextBox2)
Val extracts the numeric value in the text boxes
You may also need to trap errors in case there is no numeric value in one or other text.
 
Oops, I meant
TextBox4 = Val(TextBox3) + Val(TextBox2)
 
I figured that it was reasonable to point to the actual function required and let OP use VB Help - that way at least he learns something!

Good Luck
johnwm
________________________________________________________
To get the best from these forums read faq731-376 before posting

Steam Engine enthusiasts:
 
You should spend a bit of time reading up on data types. Every type is different, occupies different memory volume, and is manipulated in different ways.

Strictly speaking, they do not intermingle. However, the "Variant" type does permit "cross-breeding."

If you're working inside Excel, you should be particularly cognizant of this. Many an error has arisen because the operator sees something as a number while the program is viewing it as text.

--------------------
Bring back the HP-15
--------------------
 
I think that
TextBox4 = 1*(TextBox3) + 1*(TextBox2)
works too.
m777182
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor