Results 1 to 5 of 5

Thread: [Access 2003] Cannot seem to add/compare properly?

  1. #1

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    [Access 2003] Cannot seem to add/compare properly?

    I have some data in a table...

    Net : 37673.88
    VAT : 5701.42
    Gross : 43375.3

    Net + Vat = Gross.... at least it looks like it for all intents and purposes... even recoded into recordset to ensure the values...

    Access is saying that the two numbers do not match.


    What???!

    What is the best way to fix this?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [Access 2003] Cannot seem to add/compare properly?

    That is the nature of floating numbers (Double or Single)!
    You don't have that problem if 3 fields have Currency data type.
    Code:
    Sub X1()
        Dim net As Double
        Dim VAT As Double
        Dim Gross As Double
    
        net = 37673.88
        VAT = 5701.42
        Gross = 43375.3
        
        Debug.Print Gross, net + VAT, (Gross = net + VAT), Gross - (net + VAT)
        'return:  43375.3       43375.3      False          5.45696821063757E-12
    End Sub
    
    Sub X2()
        Dim net As Currency
        Dim VAT As Currency
        Dim Gross As Currency
    
        net = 37673.88
        VAT = 5701.42
        Gross = 43375.3
        
        Debug.Print Gross, net + VAT, (Gross = net + VAT), Gross - (net + VAT)
        'return:  43375.3       43375.3      True           0
    End Sub
    You should not store a calculated field in the table.
    Gross always can be calculated from net and VAT.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: [Access 2003] Cannot seem to add/compare properly?

    You should not store a calculated field in the table.
    Gross always can be calculated from net and VAT.
    Yeah I know that, but this is to hold what the other company have on an invoice.. ergo need to hold it. And in this case they mistyped one gross and were a grand under.... doh!

    Weird over the double thing, cause I took them in as text, converted to double, formatted to two decimals. The still didn't match.

    In the end I did all the conversions above, then took the total from the gross, using abs function and if this was <1 I ignored it. Hmm might need to amend that to 0.01

    Still does teh job.


    Thanks

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [Access 2003] Cannot seem to add/compare properly?

    Why do you have to convert them to Double but not Currency? That is the same process.

    If you cannot convert them to Currency then that is a good idea to check:
    If Abs(Gross - (net + VAT)) < 0.01 Then

    But this may be better:
    If Round(Gross - (net + VAT), 2) = 0 Then
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: [Access 2003] Cannot seem to add/compare properly?

    Hiya,

    I dont see why it should make a difference - if I put em in double they should still be the same and not change, otherwise converting to double, then debug.printing would give different values. It doesn't.

    Just because I dont use currency. A bit like I like to use on error resume next (ok so I have cut down after some errors snuck through and most of the data was ok - enough to fool. So only use it in dire circumstances.

    doesn't make much difference... :/

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width