Problem adding currency in VB6

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Smith

    #1

    Problem adding currency in VB6

    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
  • Steven Smith

    #2
    Re: Problem adding currency in VB6

    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 <sluke4@webbox. com> wrote in
    news:l74ag.2516 5$Gg.2267@twist er.nyroc.rr.com :
    [color=blue]
    > 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
    >[/color]

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Problem adding currency in VB6

      "Steven Smith" <sluke4@webbox. com> schrieb:[color=blue]
      > [VB6-related question][/color]

      Note that the group "microsoft.publ ic.dotnet.langu ages.vb" is related to
      VB.NET. VB6 groups can be found in the "microsoft.publ ic.vb.*" hierarchy.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      • John

        #4
        Re: Problem adding currency in VB6

        Try converting the text values to numbers before adding them.

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



        On Mon, 15 May 2006 19:05:42 GMT, Steven Smith <sluke4@webbox. com>
        wrote:
        [color=blue]
        >I should probably show you the code in question.....
        >
        >
        >Dim AmountDue As Currency
        > AmountDue = Item1Amt.Text + Item2Amt.Text + Item3Amt.Text +
        >Item4Amt.Tex t + Item5Amt.Text + Item6Amt.Text + Item7Amt.Text +
        >Item8Amt.Tex t - Item9Amt.Text - ItemAAmt.Text
        >
        >
        >
        >
        >Thanks again...
        >
        >
        >Steven Smith <sluke4@webbox. com> wrote in
        >news:l74ag.251 65$Gg.2267@twis ter.nyroc.rr.co m:
        >[color=green]
        >> 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
        >>[/color][/color]

        Comment

        • Steven Smith

          #5
          Re: Problem adding currency in VB6

          Thanks John.... that worked perfectly.

          Ah, the joys of being a newbie!



          Steve


          John <look@sig.net > wrote in news:2qnh62tgjm p4j2v2rgoe3li1g 89ogdh014@
          4ax.com:
          [color=blue]
          > Try converting the text values to numbers before adding them.
          >
          > AmountDue = val(Item1Amt.Te xt) + val(Item2Amt.Te xt) +
          > val(Item3Amt.Te xt) + val(Item4Amt.Te xt) + val(Item5Amt.Te xt) +
          > val(Item6Amt.Te xt) + VItem7Amt.Text) + val(Item8Amt.Te xt) -
          > val(Item9Amt.Te xt) - val(ItemAAmt.Te xt)
          >
          >
          >
          > On Mon, 15 May 2006 19:05:42 GMT, Steven Smith <sluke4@webbox. com>
          > wrote:
          >[color=green]
          >>I should probably show you the code in question.....
          >>
          >>
          >>Dim AmountDue As Currency
          >> AmountDue = Item1Amt.Text + Item2Amt.Text + Item3Amt.Text +
          >>Item4Amt.Te xt + Item5Amt.Text + Item6Amt.Text + Item7Amt.Text +
          >>Item8Amt.Te xt - Item9Amt.Text - ItemAAmt.Text
          >>
          >>
          >>
          >>
          >>Thanks again...
          >>
          >>
          >>Steven Smith <sluke4@webbox. com> wrote in
          >>news:l74ag.25 165$Gg.2267@twi ster.nyroc.rr.c om:
          >>[color=darkred]
          >>> Hi,
          >>>
          >>> I'm trying to write a simple program to print invoices for people I[/color][/color][/color]
          do[color=blue][color=green][color=darkred]
          >>> 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[/color][/color][/color]
          a[color=blue][color=green][color=darkred]
          >>> 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
          >>>[/color][/color]
          >
          >[/color]

          Comment

          • Michael D. Ober

            #6
            Re: Problem adding currency in VB6

            A better solution would be:

            Dim AmountDue as Currency
            AmountDue = 0
            if isnumeric(item1 Amt.text) then amountDue = AmountDue + ccur(item1amt.t ext)
            if isnumeric(item2 Amt.text) then amountDue = AmountDue + ccur(item2amt.t ext)
            if isnumeric(item3 Amt.text) then amountDue = AmountDue + ccur(item3amt.t ext)
            if isnumeric(item4 Amt.text) then amountDue = AmountDue + ccur(item4amt.t ext)
            if isnumeric(item5 Amt.text) then amountDue = AmountDue + ccur(item5amt.t ext)
            if isnumeric(item6 Amt.text) then amountDue = AmountDue + ccur(item6amt.t ext)
            if isnumeric(item7 Amt.text) then amountDue = AmountDue + ccur(item7amt.t ext)
            if isnumeric(item8 Amt.text) then amountDue = AmountDue + ccur(item8amt.t ext)
            if isnumeric(item9 Amt.text) then amountDue = AmountDue - ccur(item9amt.t ext)
            if isnumeric(itemA Amt.text) then amountDue = AmountDue - ccur(itemAamt.t ext)

            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.

            "John" <look@sig.net > wrote in message
            news:2qnh62tgjm p4j2v2rgoe3li1g 89ogdh014@4ax.c om...[color=blue]
            > Try converting the text values to numbers before adding them.
            >
            > AmountDue = val(Item1Amt.Te xt) + val(Item2Amt.Te xt) +
            > val(Item3Amt.Te xt) + val(Item4Amt.Te xt) + val(Item5Amt.Te xt) +
            > val(Item6Amt.Te xt) + VItem7Amt.Text) + val(Item8Amt.Te xt) -
            > val(Item9Amt.Te xt) - val(ItemAAmt.Te xt)
            >
            >
            >
            > On Mon, 15 May 2006 19:05:42 GMT, Steven Smith <sluke4@webbox. com>
            > wrote:
            >[color=green]
            > >I should probably show you the code in question.....
            > >
            > >
            > >Dim AmountDue As Currency
            > > AmountDue = Item1Amt.Text + Item2Amt.Text + Item3Amt.Text +
            > >Item4Amt.Tex t + Item5Amt.Text + Item6Amt.Text + Item7Amt.Text +
            > >Item8Amt.Tex t - Item9Amt.Text - ItemAAmt.Text
            > >
            > >
            > >
            > >
            > >Thanks again...
            > >
            > >
            > >Steven Smith <sluke4@webbox. com> wrote in
            > >news:l74ag.251 65$Gg.2267@twis ter.nyroc.rr.co m:
            > >[color=darkred]
            > >> 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
            > >>[/color][/color]
            >
            >[/color]



            Comment

            Working...