Results 1 to 12 of 12
  1. #1
    rbee is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    7

    divide by zero

    Hi,
    I am pretty new to access in general so forgive me because I have seen this posted elsewhere but do not know how to apply it to my situation.

    I am essentially trying to do a-b/b, however b can be null or zero.

    round(abs(((im.metric_val-i.bid)/i.bid)*100),3) AS Abs_Pct_Diff

    if i.bid is null or zero I'm okay with having it return 0.

    I have seen several suggestions to try nz or IIF statements but I cannot write them properly given my query.

    Any help is appreciated,
    Thanks!

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    How about a compound IIF? WARNING - UNTESTED
    IIF bid = 0, 0, IIF IsNull(bid),0,round(abs(((im.metric_val-i.bid)/i.bid)*100),3) AS Abs_Pct_Diff

  3. #3
    rbee is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    7
    As written I get this error:

    IIF function without () in query expression 'IIF bid='

    I've tried adding parenthesis in a varity of ways but I cannot seem to find the right place for them.

  4. #4
    rbee is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    7
    This ran, still getting the divide by 0 error.

    IIF(i.bid = '0', 0), IIF(IsNull(i.bid),0),round(abs(((im.metric_val-i.bid)/i.bid)*100),3) AS Abs_Pct_Diff,

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I'm thinking that an Immediate IF (IIF) may test all of the conditions but maybe not. Is your field [bid] numeric? If so then IIF(i.bid = '0', 0) should be IIF(i.bid = 0, 0)!

  6. #6
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    My suggestion assuming i.bid is numeric.

    IIF(i.bid = 0 Or isnull(i.bid), 0, round(abs(((im.metric_val-i.bid)/i.bid)*100),3)) AS Abs_Pct_Diff

  7. #7
    rbee is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    7
    bid data type is integer. Removed the quotes.. still getting the division by zero error.

    I am thinking, maybe, the tables I am trying to join do not always have matches so when that happens it causes the division by zero error. So if I take the ID from the first table, it does not find a match in the second table, and tries to divide by zero.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You may need to use a User Defined Function (UDF) here.

  9. #9
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I am thinking, maybe, the tables I am trying to join do not always have matches so when that happens it causes the division by zero error. So if I take the ID from the first table, it does not find a match in the second table, and tries to divide by zero.
    I recreated your scenario, and this seemed to work well for me.
    Code:
    Abs_Pct_Diff: IIf(IsNull(i.bid),0,Round(Abs(((im.metric_val-i.bid)/i.bid)*100),3))

  10. #10
    rbee is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    7
    No actually, there are 0's entered for [bid].

    I cannot figure out an IF or nz statement to bypass them.. nothing I am trying is working.

  11. #11
    rbee is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    7
    Trying this now.

  12. #12
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Sorry, I missed the fact that you have Nulls and Zeroes to contend with. I added some zeroes, and this seemed to handle both cases:
    Code:
    Abs_Pct_Diff: IIf(nz(i.bid,0)=0,0,Round(Abs(((im.metric_val-i.bid)/i.bid)*100),3))

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

Similar Threads

  1. divide value by a constant and populate same field
    By ridgeview80 in forum Queries
    Replies: 5
    Last Post: 05-28-2012, 09:37 AM
  2. Divide a column by another Column in a Report?
    By taimysho0 in forum Reports
    Replies: 2
    Last Post: 01-06-2012, 06:25 PM
  3. Divide two Fields, Total in another field
    By prawln in forum Access
    Replies: 1
    Last Post: 03-28-2011, 06:45 PM
  4. Replies: 0
    Last Post: 03-18-2010, 01:31 PM
  5. Replies: 1
    Last Post: 04-09-2009, 09:18 AM

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