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 Numbers

Status
Not open for further replies.

Vec

IS-IT--Management
Jan 29, 2002
418
0
0
US
To add two numeric values from two text boxes and place the answer in a third text box, I use:
Text3.Text = Val(Text1.Text) + Val(Text2.Text) But what if the values of Text1 and Text2 contain a $ sign? For example $50.00 instead of 50.00, this makes it not numeric, but I still need to add them together.

So if Text1.Text contains the string "$50.00" and Text2.Text contains the string "$50.00" what statement do I use in a cmd button to make Text3.Text = "$100.00" when cmd is pressed?

Using Text1.Text = Val(Text2.Text) + Val(Text3.Text) it places a zero in Text3 becuase $ is not numeric.

Is there a statement that will remove the $ before adding the numbers?

-


WebFusion Design
Looking Toward The Future
 
Vec-

Well technically, in your example above the value "$50.00" is numeric. In that, the the following call to the VB function IsNumeric will return a value of true:

IsNumeric("$50.00")

Your problem is that, although "$50.00" is numeric, it is not a Double, which is the type of value returned from the function Val. "$50.00" is an example of the numeric data type Currency.

If you replace your call to the function Val with the function CCur then you should be able to handle any numeric value passed in, even $50.00

Example:
Text1.Text = CCur(Text2.Text) + CCur(Text3.Text)

Of course, this does assume that the values in Text2 & Text3 are numeric. If "$6u.9k" is in text2 when the above line is executed then an error will be generated.

You'll also need to do some standard formatting of the value in text1 if you need it to have the appearance of currency when it is displayed.

For example:
If Text2 contains $50.00 and Text3 also contains $50.00 then without any formatting the value in text1 after executing the following line will be "100", not "$100.00":

Text1.Text = CCur(Text2.Text) + CCur(Text3.Text)


Hope this helps!
Josh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top