Results 1 to 8 of 8
  1. #1
    DB2010MN26 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Sep 2010
    Posts
    37

    Calculation in VB Code of Currency off by cents

    I'm performing a calculation in the after update event of a datasheet form which I'm getting an answer about 20 cents off. Do I need to convert the numbers to something else in my subroutine?



    actual table values:
    costbasisunitprice = 12.38
    CurrentPrice = 11.49
    TradeAmt = 59399.03

    (CurrentPrice - costbasisunitprice) * (TradeAmt / CurrentPrice) = 4600.97

    The answer I get with my code = 4600.77

    my code:

    Private Sub TradeAmt_AfterUpdate()
    [TradeTax] = ([CurrentPrice] - [costbasisunitprice]) * ([TradeAmt] / [CurrentPrice])
    End Sub

  2. #2
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    When I do the calculation manually, I get 4600.99 so I am guessing that this is a rounding or truncation issue somewhere in your db. What are your fields formatted as? Numbers? Long Integer, Single, Double, Decimal? This will make a difference possibly

  3. #3
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Try the following sample using your figures (I get -4600.96925152307 )

    Code:
    Sub DecimalPlaceIssues()
    Dim costbasisunitprice As Double
    Dim CurrentPrice As Double
    Dim TradeAmt As Double
    Dim TradeTax As Double
    costbasisunitprice = 12.38
    CurrentPrice = 11.49
    TradeAmt = 59399.03
    '
    '(CurrentPrice - costbasisunitprice) * (TradeAmt / CurrentPrice) = 4600.97'
    '
    'The answer I get with my code = 4600.77
    
    myCode:
    [TradeTax] = ([CurrentPrice] - [costbasisunitprice]) * ([TradeAmt] / [CurrentPrice])
    Debug.Print TradeTax
    End Sub

  4. #4
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Something you have given is not right - your result should be negative, since (CurrentPrice - costbasisunitprice) = -.89, making the whole expression negative (-4600.97).

    Try putting a few debug.print statements into your subroutine to verify the values are actually what you think they should be. You could also try using the Val() function with each of your fields, to ensure they really are numeric (e.g. Val([CurrentPrice]) )

    HTH

    John

  5. #5
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That should work. My first guess is that one or more of those numbers is actually something else in the data, and you're only seeing that. One way to test:

    [TradeTax] = (Round([CurrentPrice],2) - Round([costbasisunitprice],2)) * (Round([TradeAmt],2) / Round([CurrentPrice],2))
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    DB2010MN26 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Sep 2010
    Posts
    37
    Quote Originally Posted by John_G View Post
    Something you have given is not right - your result should be negative, since (CurrentPrice - costbasisunitprice) = -.89, making the whole expression negative (-4600.97).
    John
    Sorry about that, my answer was negotive, just off by the cents.

    I tryed the Val( function but still got the same answer. Looks like the issue was rounding since I was able to get (-4600.97) when I used pbaldy's example:


    [TradeTax] = (Round([CurrentPrice],2) - Round([costbasisunitprice],2)) * (Round([TradeAmt],2) / Round([CurrentPrice],2))

    Also for reference, my tables had the data formated as currency with decimal places set to 2.

    Thanks everyone for your help and fast response!

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Quote Originally Posted by DB2010MN26 View Post
    Sorry about that, my answer was negotive, just off by the cents.

    I tryed the Val( function but still got the same answer. Looks like the issue was rounding since I was able to get (-4600.97) when I used pbaldy's example:


    [TradeTax] = (Round([CurrentPrice],2) - Round([costbasisunitprice],2)) * (Round([TradeAmt],2) / Round([CurrentPrice],2))

    Also for reference, my tables had the data formated as currency with decimal places set to 2.

    Thanks everyone for your help and fast response!
    I'd use more that 2 decimal places for the intermediate calculations.
    Use Double data type, and report in Currency if that's what you need.

  8. #8
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    My suggestion was intended to identify the problem, which appears to be the values in the table. In other words, you may be seeing

    CurrentPrice = 11.49

    but what's actually in the table is

    CurrentPrice = 11.493

    Depending on your specific needs, you may want to round values as they are entered into the table to prevent this type of thing in the future.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Country Code Calculation
    By nchesebro in forum Forms
    Replies: 14
    Last Post: 06-15-2011, 02:03 PM
  2. Currency
    By Merv in forum Access
    Replies: 3
    Last Post: 06-04-2011, 08:05 PM
  3. Replies: 2
    Last Post: 03-23-2011, 11:43 AM
  4. Currency Vs Double
    By Mclaren in forum Database Design
    Replies: 1
    Last Post: 08-12-2010, 05:38 PM
  5. Calculation is off by a few cents...
    By Jule in forum Reports
    Replies: 3
    Last Post: 05-01-2006, 01:06 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