Results 1 to 5 of 5

Thread: [RESOLVED] access vba: problem with condition in if statement

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    33

    Resolved [RESOLVED] access vba: problem with condition in if statement

    Hi

    I have a string array called tokens that has been previously filled. I convert some of the tokens to doubles and add them together, but sometimes it seems to evaluate incorrectly. I have a case where it should evaluate to true, but is evaluating to false for some reason I can't understand. Any ideas on what I'm missing or what I can do to fix this? Last time I checked 2.73+17.51=20.24 was a true statement... Is VBA just retarded!?

    VB Code:
    1. 'try the new algorithm to fill the variables
    2.         If ((CDbl(tokens(3)) + CDbl(tokens(5))) = CDbl(tokens(1))) Then
    3.         'Why isn't this evaluating to true on record 8 for 9730 - 7267
    4.         'tokens(3) = 2.73
    5.         'tokens(5) = 17.51
    6.         'tokens(1) = 20.24
    7.             paidToAgency = tokens(1)
    8.             paidToYou = 0
    9.         Else
    10.             'Now try to figure it out based on tabs...
    11.             If (CInt(Mid(tokens(0), 3)) > CInt(Mid(tokens(2), 3))) Then
    12.                 paidToYou = tokens(1)
    13.                 paidToAgency = 0
    14.             Else
    15.                 paidToAgency = tokens(1)
    16.                 paidToYou = 0
    17.                 tableName = "tblBadData"
    18.             End If
    19.         End If

    Here is a copy of my immediate window during execution:

    Code:
    ?(CDbl(tokens(3)) + CDbl(tokens(5))) = CDbl(tokens(1))
    False
    ?tokens(3)
    2.73
    ?tokens(5)
    17.51
    ?tokens(1)
    20.24
    Thanks,
    Ranthalion

  2. #2
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: access vba: problem with condition in if statement

    Try
    ?format$(Cdbl(tokens(3)) + Cdbl(tokens(5)),"00.00000000000")
    ?format$(Cdbl(tokens(1)),"00.00000000000")

    Doubles are notorious for their rounding problems maybe your code trippes of this.
    Last edited by Dnereb; Jun 21st, 2006 at 04:18 PM. Reason: Adding Cdbl() to the code
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: access vba: problem with condition in if statement

    Almost ANY decimal-to-binary-to-decimal conversion using complex numbers (numbers with decimals) will have rounding errors. (You can choose a few that won't, but most will, which is why BCD arithmetic was invented.) Never test for equality, test for the difference being smaller than what you care about - < 0.01 in this case.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    33

    Re: access vba: problem with condition in if statement

    Thanks. I was aware of accuracy issues with doubles, but I figured that since it was given a decimal with only 2 decimal points that it wouldn't be a problem. :-) Guess I was wrong... First time I've ever run into an issue with doubles actually messing anything up, but it was a good lesson learned. Once again, thanks for the help.

  5. #5
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: [RESOLVED] access vba: problem with condition in if statement

    A way to solve your problem is to use currency instead of doubles, if you just need two decimals.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

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