Results 1 to 4 of 4
  1. #1
    BillDodd is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2021
    Posts
    1

    VB6 calculation error Single variable v double variable v entered numerical value

    In a table, the variable RetailPrice is a single value - displayed on a form say value £1.67 It has to be discounted and the result needs to be rounded to 2 sig figs before any other calulation is applied to it. DiscPerc is a single variable value of 50.00. Qty is a single variable value of 44. This gives the DiscAmt and the LineNet. This is in an button click event subroutine:-




    Dim Qty, DiscPerc, DiscAmt, LineNet As Single
    Dim DoubleRetailPrice As Double

    Qty = Me.txtQty
    DiscPerc = Me.txtDiscount
    DoubleRetailPrice = RetailPrice + 0.000001

    DiscAmt = Qty * Round((RetailPrice * (DiscPerc / 100)), 2)
    LineNet = Qty * Round((RetailPrice * ((100 - DiscPerc) / 100)), 2)
    LineNet = Round(LineNet, 2)


    This should give a value of 0.84 * 44 = 36.96
    However its gives a value of 0.83 * 44 = 36.52

    By adding a line above and Defining DoubleRetailPrice as a double - as below:-

    DiscAmt = Qty * Round((DoubleRetailPrice * (DiscPerc / 100)), 2)
    LineNet = Qty * Round((DoubleRetailPrice * ((100 - DiscPerc) / 100)), 2)
    LineNet = Round(LineNet, 2)


    DoubleRetailPrice = 1.66999995708466 which when multiplied by 44 gives 36.52 - again wrong

    However if I add this code instead

    DoubleRetailPrice = RetailPrice + 0.000001 ( I get a value of 1.670001 which actually gives the correct calulation if you replace the variable RetailPrice with DoubleRetailPrice)

    In the immediate section in VB window ;-

    ? Round(0.12335,4) = 0.1234 ( what you would expect, so rounding is correct )
    ? 1.67 = RetailPrice gives True
    ? Round( ( RetailPrice * ( DiscPerc / 100 ) ) ,2) gives 0.83
    ? Round( ( 1.67 * ( DiscPerc / 100 ) ) ,2) gives 0.84


    Surely there must be an error in VB6?

    I have created a database to show this if anyone wants it.testdb.accdb

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    Try specifying your numbers as decimal (18,4) or currency (I can't work out if you are doing this in Access or VB).

    In vba this line
    Dim Qty, DiscPerc, DiscAmt, LineNet As Single

    Would Dim Qty, DiscPerc, DiscAmt as variants.
    You have to declare them specifically
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    More info and tricks for rounding
    http://allenbrowne.com/round.html
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 07-23-2021, 10:32 AM
  2. Replies: 2
    Last Post: 05-14-2021, 08:39 AM
  3. Replies: 2
    Last Post: 07-23-2019, 01:37 PM
  4. Error 91 Object Variable or Block Variable Not Set
    By mindbender in forum Programming
    Replies: 5
    Last Post: 05-01-2017, 12:01 PM
  5. Replies: 6
    Last Post: 02-11-2016, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums