Home All Groups Group Topic Archive Search About

Problem adding currency in VB6

Author
15 May 2006 6:54 PM
Steven Smith
Hi,

I'm trying to write a simple program to print invoices for people I do work
for. I've got a form with textboxes for descriptions and amounts for items,
and some code for printing the invoices. It all works except for the one
line that adds all of the item amounts. At run time, a "type mismatch"
error is thrown. I've tried a couple of different things, but the code
won't work. Is there something special about adding currency I'm missing?
I've also tried making everything a string, and when I add amounts, it will
just concatenate the strings. Also, this method won't let me subtract
discounts.

Any help or code snippets would be greatly appreciated.

Thanks,


Steven Smith

Author
15 May 2006 7:05 PM
Steven Smith
I should probably show you the code in question.....


Dim AmountDue As Currency
    AmountDue = Item1Amt.Text + Item2Amt.Text + Item3Amt.Text +
Item4Amt.Text + Item5Amt.Text + Item6Amt.Text + Item7Amt.Text +
Item8Amt.Text - Item9Amt.Text - ItemAAmt.Text




Thanks again...


Steven Smith <slu***@webbox.com> wrote in
Show quoteHide quote
news:l74ag.25165$Gg.2267@twister.nyroc.rr.com:

> Hi,
>
> I'm trying to write a simple program to print invoices for people I do
> work for. I've got a form with textboxes for descriptions and amounts
> for items, and some code for printing the invoices. It all works
> except for the one line that adds all of the item amounts. At run
> time, a "type mismatch" error is thrown. I've tried a couple of
> different things, but the code won't work. Is there something special
> about adding currency I'm missing? I've also tried making everything a
> string, and when I add amounts, it will just concatenate the strings.
> Also, this method won't let me subtract discounts.
>
> Any help or code snippets would be greatly appreciated.
>
> Thanks,
>
>
> Steven Smith
>
Author
15 May 2006 7:48 PM
Herfried K. Wagner [MVP]
"Steven Smith" <slu***@webbox.com> schrieb:
> [VB6-related question]

Note that the group "microsoft.public.dotnet.languages.vb" is related to
VB.NET.  VB6 groups can be found in the "microsoft.public.vb.*" hierarchy.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
15 May 2006 8:08 PM
John
Try converting the text values to numbers before adding them.

   AmountDue = val(Item1Amt.Text) + val(Item2Amt.Text) +
val(Item3Amt.Text) + val(Item4Amt.Text) + val(Item5Amt.Text) +
val(Item6Amt.Text) + VItem7Amt.Text) + val(Item8Amt.Text) -
val(Item9Amt.Text) - val(ItemAAmt.Text)



On Mon, 15 May 2006 19:05:42 GMT, Steven Smith <slu***@webbox.com>
wrote:

Show quoteHide quote
>I should probably show you the code in question.....
>
>
>Dim AmountDue As Currency
>    AmountDue = Item1Amt.Text + Item2Amt.Text + Item3Amt.Text +
>Item4Amt.Text + Item5Amt.Text + Item6Amt.Text + Item7Amt.Text +
>Item8Amt.Text - Item9Amt.Text - ItemAAmt.Text
>
>
>
>
>Thanks again...
>
>
>Steven Smith <slu***@webbox.com> wrote in
>news:l74ag.25165$Gg.2267@twister.nyroc.rr.com:
>
>> Hi,
>>
>> I'm trying to write a simple program to print invoices for people I do
>> work for. I've got a form with textboxes for descriptions and amounts
>> for items, and some code for printing the invoices. It all works
>> except for the one line that adds all of the item amounts. At run
>> time, a "type mismatch" error is thrown. I've tried a couple of
>> different things, but the code won't work. Is there something special
>> about adding currency I'm missing? I've also tried making everything a
>> string, and when I add amounts, it will just concatenate the strings.
>> Also, this method won't let me subtract discounts.
>>
>> Any help or code snippets would be greatly appreciated.
>>
>> Thanks,
>>
>>
>> Steven Smith
>>
Author
15 May 2006 9:16 PM
Steven Smith
Thanks John.... that worked perfectly.

Ah, the joys of being a newbie!



Steve


John <l***@sig.net> wrote in news:2qnh62tgjmp4j2v2rgoe3li1g89ogdh014@
4ax.com:

Show quoteHide quote
> Try converting the text values to numbers before adding them.
>
>    AmountDue = val(Item1Amt.Text) + val(Item2Amt.Text) +
> val(Item3Amt.Text) + val(Item4Amt.Text) + val(Item5Amt.Text) +
> val(Item6Amt.Text) + VItem7Amt.Text) + val(Item8Amt.Text) -
> val(Item9Amt.Text) - val(ItemAAmt.Text)
>
>
>
> On Mon, 15 May 2006 19:05:42 GMT, Steven Smith <slu***@webbox.com>
> wrote:
>
>>I should probably show you the code in question.....
>>
>>
>>Dim AmountDue As Currency
>>    AmountDue = Item1Amt.Text + Item2Amt.Text + Item3Amt.Text +
>>Item4Amt.Text + Item5Amt.Text + Item6Amt.Text + Item7Amt.Text +
>>Item8Amt.Text - Item9Amt.Text - ItemAAmt.Text
>>
>>
>>
>>
>>Thanks again...
>>
>>
>>Steven Smith <slu***@webbox.com> wrote in
>>news:l74ag.25165$Gg.2267@twister.nyroc.rr.com:
>>
>>> Hi,
>>>
>>> I'm trying to write a simple program to print invoices for people I
do
>>> work for. I've got a form with textboxes for descriptions and amounts
>>> for items, and some code for printing the invoices. It all works
>>> except for the one line that adds all of the item amounts. At run
>>> time, a "type mismatch" error is thrown. I've tried a couple of
>>> different things, but the code won't work. Is there something special
>>> about adding currency I'm missing? I've also tried making everything
a
>>> string, and when I add amounts, it will just concatenate the strings.
>>> Also, this method won't let me subtract discounts.
>>>
>>> Any help or code snippets would be greatly appreciated.
>>>
>>> Thanks,
>>>
>>>
>>> Steven Smith
>>>
>
>
Author
16 May 2006 4:13 AM
Michael D. Ober
A better solution would be:

Dim AmountDue as Currency
AmountDue = 0
if isnumeric(item1Amt.text) then amountDue = AmountDue + ccur(item1amt.text)
if isnumeric(item2Amt.text) then amountDue = AmountDue + ccur(item2amt.text)
if isnumeric(item3Amt.text) then amountDue = AmountDue + ccur(item3amt.text)
if isnumeric(item4Amt.text) then amountDue = AmountDue + ccur(item4amt.text)
if isnumeric(item5Amt.text) then amountDue = AmountDue + ccur(item5amt.text)
if isnumeric(item6Amt.text) then amountDue = AmountDue + ccur(item6amt.text)
if isnumeric(item7Amt.text) then amountDue = AmountDue + ccur(item7amt.text)
if isnumeric(item8Amt.text) then amountDue = AmountDue + ccur(item8amt.text)
if isnumeric(item9Amt.text) then amountDue = AmountDue - ccur(item9amt.text)
if isnumeric(itemAAmt.text) then amountDue = AmountDue - ccur(itemAamt.text)

Also, use the CCur function to convert from the text box string values to
currency - you'll avoid rounding errors that the Val function can introduce.
Also note that VB 6 implictely uses the .TEXT property if you don't, but VB
2002 and later require it.  It makes your code more specific so it's a
decent habit to be in anyway.

Mike Ober.

Show quoteHide quote
"John" <l***@sig.net> wrote in message
news:2qnh62tgjmp4j2v2rgoe3li1g89ogdh014@4ax.com...
> Try converting the text values to numbers before adding them.
>
>    AmountDue = val(Item1Amt.Text) + val(Item2Amt.Text) +
> val(Item3Amt.Text) + val(Item4Amt.Text) + val(Item5Amt.Text) +
> val(Item6Amt.Text) + VItem7Amt.Text) + val(Item8Amt.Text) -
> val(Item9Amt.Text) - val(ItemAAmt.Text)
>
>
>
> On Mon, 15 May 2006 19:05:42 GMT, Steven Smith <slu***@webbox.com>
> wrote:
>
> >I should probably show you the code in question.....
> >
> >
> >Dim AmountDue As Currency
> >    AmountDue = Item1Amt.Text + Item2Amt.Text + Item3Amt.Text +
> >Item4Amt.Text + Item5Amt.Text + Item6Amt.Text + Item7Amt.Text +
> >Item8Amt.Text - Item9Amt.Text - ItemAAmt.Text
> >
> >
> >
> >
> >Thanks again...
> >
> >
> >Steven Smith <slu***@webbox.com> wrote in
> >news:l74ag.25165$Gg.2267@twister.nyroc.rr.com:
> >
> >> Hi,
> >>
> >> I'm trying to write a simple program to print invoices for people I do
> >> work for. I've got a form with textboxes for descriptions and amounts
> >> for items, and some code for printing the invoices. It all works
> >> except for the one line that adds all of the item amounts. At run
> >> time, a "type mismatch" error is thrown. I've tried a couple of
> >> different things, but the code won't work. Is there something special
> >> about adding currency I'm missing? I've also tried making everything a
> >> string, and when I add amounts, it will just concatenate the strings.
> >> Also, this method won't let me subtract discounts.
> >>
> >> Any help or code snippets would be greatly appreciated.
> >>
> >> Thanks,
> >>
> >>
> >> Steven Smith
> >>
>
>