Results 1 to 4 of 4
  1. #1
    robertagc is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2013
    Posts
    2

    I'm having trouble seeing the error in this expression

    I'm trying to write an expression that circumvents a divide by zero error. Expr4 and 5 may or may be Null, Zero or an integer greater than 0. I want to evaluate Expr4 and 5 together in Expr2. I've been trying to work with some version of the following all day, but Access keeps defaulting to the original expression I had stored.

    The original expression reads as follows:


    Expr2:iif([datum1]+[datum2]+[datum3]+[datum4]=0,0,IIf([datum3] Is Null,[datum1]/[datum2],IIf([datum1] Is Null,[datum3]/[datum4],([datum3]+[datum1])/([datum4]+[datum2]))))

    This one works, but I get an error if Datum 1 or 3 is Null and the other Datum equal 0. I tried fudging around with this expression to massage-out the error, and it just became impossible to manage. When I tried to do was create two new expression to manage datum 1 and 2 together and datum 3 and 4 together. I figured I could condense the work of those two expressions into a new Expr 2 as follows:

    Expr2: iif(([Expr4] is null) AND ([Expr5] is null),Null,(iif(([Expr4] is null) AND ([Expr5] is not null),[Expr5],(iif(([Expr5] is null) AND ([Expr4] is not null),[Expr4],(iif(([Expr4]+[Expr5])=0,0,(([Expr4]+[Expr5]/2)))))

    When I put this into the expression builder or copy the expression directly into the query design, Access reverts back to the original expression. There must be something wrong with it, and it's probably breathtakingly simple, but I don't see it. Would somebody mind seeing what I am clearly incapable of seeing?

    Thanks,


    -Rob

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Simple arithemetic with Null should result in Null.

    What is the exact error on the original expression?

    Too many parens in the second. Think might need IsNull instead of Is Null. Review http://allenbrowne.com/casu-12.html

    Expr2: IIf(IsNull([Expr4]) AND IsNull([Expr5]), Null, IIf(IsNull([Expr4]) AND Not IsNull([Expr5]), [Expr5], IIf(IsNull([Expr5]) AND Not IsNull([Expr4]), [Expr4], IIf([Expr4]+[Expr5]=0,0, [Expr4]+[Expr5]/2))))

    Do you want to divide Expr5 by 2 or the sum of Expr4 and Expr5 by 2?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    qa12dx is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    india
    Posts
    112
    Quote Originally Posted by robertagc View Post
    I'm trying to write an expression that circumvents a divide by zero error.
    i think u answered your own q. but in ur expr u didn't.

    try;
    iif([datum1]+[datum2]+[datum3]+[datum4]=0,0,
    IIf([datum3] Is Null and [datum2]>0,[datum1]/[datum2],
    IIf([datum1] Is Null and [datum4]>0,[datum3]/[datum4],
    ([datum3]+[datum1])/([datum4]+[datum2]))))

    best is to not work with nulls. if in your table set the default value of the datum fields to zero. for the existing records run an update qry to set them to 0. when you don't have to mess with nulls in calcs, running calcs become infinitly easier...

    if u run append queries and the fields get set as null, run update queries to set to 0 before running the calc qrys.

  4. #4
    robertagc is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2013
    Posts
    2
    June, that was perfect! Thanks. Also, yes, I want to divide the sum of 4 and 5 by 2, not 5 by 2. I was missing a parenthetical set.

    qa12dx, I agree. Working with Nulls is a pain. When I build dbs for my own use, I avoid them. However, this was for work, and the situation is such where Nulls are just part of the job.

    Thanks again!
    -Rob
    Last edited by robertagc; 03-26-2013 at 08:56 AM. Reason: spelling error

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

Similar Threads

  1. Replies: 6
    Last Post: 05-30-2012, 12:32 PM
  2. Replies: 1
    Last Post: 05-11-2012, 10:59 AM
  3. trouble understanding expression
    By mejia.j88 in forum Queries
    Replies: 4
    Last Post: 02-01-2012, 03:00 PM
  4. Trouble with Expression
    By kctxucop08 in forum Access
    Replies: 1
    Last Post: 07-14-2011, 01:55 AM
  5. Expression trouble
    By ROB in forum Access
    Replies: 2
    Last Post: 06-08-2010, 09:36 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