Link to home
Start Free TrialLog in
Avatar of David Svedarsky
David SvedarskyFlag for United States of America

asked on

Sum of multiple TextBox Currency values

Hi Experts,

Looking for sample code on the following:

Note: All Currency values.

TextBoxA value Plus TextBoxB value = TextBoxC value
TextBoxD value minus TextBoxE value = TextBoxF value
TextBoxG value minus TextBoxH value = TextBoxI value

TestBoxJ= Sum of TextBoxes C, F, & I

Controlled by 1 ButtonClick event if possible.

Thanks,

Dave
Avatar of ZeonFlash
ZeonFlash

Avatar of David Svedarsky

ASKER

I had forgotten on the last post that I needed Currency values
SOLUTION
Avatar of gangwisch
gangwisch

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
Thanks for the response.

Everything works fine except:

TextBoxJ.text=formatcurrency(val(TextBoxC.text)+val(TextBoxF.text)+val(TextBoxI.text))

I have double checked everything, changedTextBox names etc. I can't get this line to work. No errors, just theTextBoxJ value = $0.00

I'm using Visual Studio 2005 if that makes a difference.

Dave
This suggests that the line

>>
Note: All Currency values.
<<

in your first point is wrong, or at least misleading.  

As Idle_Mind pointed out in another post, there's lots of ways of doing conversions.  But they're not all identical.  Val(<string>) will only work with a string which represents a number and nothing else.  So, if your TextBoxA shows 1.00 and your TextBoxB shows 2.50 the code provided by gangwisch will result in 3.50 and the formatcurrency function will put that in TextBoxC as $3.50 (or £3.50, or whatever else the local machine's regional settings show for currency).  But if, as the note quoted above implies should be the case, TextBoxA shows $1.00 and TextBoxB shows $2.50 the val() function will regard that as non-numeric and return 0, so what will be shown in TextBoxC will be $0.00.

Because, in testing gangwisch's code you used "pure" numbers, rather than the currency that your note said you were going to use, it worked OK for the first three additions.  But, when it started to add together the results of those first three additions, which had already been formatted as currency, it showed its true colours.

CDec() behaves differently from Val, in that it will recognise formatted currency values as well as "pure" numbers.  So try gangwisch's code substituting that for Val().  

But you ought also to think a bit about more precisely defining what it is you are trying to achieve and what the inputted data will actually be.  And that might lead you to considering whether you need to include validation checks before you start manipulating the data.

Roger
Roger,
I sincerely appreciate you and Idle Mind taking the time to steer me in the right direction.

The code change tip you gave me worked great.

I would like to clarify something:
Are you saying that gangwisch's code is correct if the correct numbers/format are entered into the textboxes?

No matter what number format I use, I can't get the last line of code to work.

Thanks again for your help,

Dave
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, I've just realised that my last post may be misleading.  CDec() does not actually ignore the leading $ if it is TYPED in to the textbox.  It only ignores it if it is introduced into the textbox with the formatcurrency function.

Roger
Roger,

I finally got it!
Now I realize why I have been having so many problems with using math in coding.

The users of this site are very fortunate to have you as a Expert.

I will split the points due to the misleading info in my post.

So it doesn't seem that I have ignored Idle Mind's comments, I had already posted this question before I received his information.

Thanks to everyone,

Dave
Avatar of Mike Tomlinson
In general, it is a baaaaaad idea to use the TextBox itslef to hold your intermediate values.

Create actual variables to hold your numeric data and convert all of your string values to numerics.  Then perform the calculations and output the results:

    Dim someVar As Decimal
    someVar = Decimal.Parse(TextBox1.Text)
    ' you can enapsulate the above in a Try...Catch
    ' allowing you to tell the user which TextBox has bad input

    ' repeat the above for the other TextBoxes...

    ' do some calculations...
    Dim result As Decimal
    result = someVarXX + someVarYY * someVarZZ

    ' output it...
    TextBox5.Text = result.ToString("whatever format you need here")

The above pattern can be used for any data type...