Results 1 to 7 of 7
  1. #1
    robinsong is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    2

    trouble with code for calculated value

    Hi, I am trying to figure out what's wrong with my code for a calculated value. I have a database for nesting birds, and I'm trying to get Access to return a value from various other fields, depending on which of those fields has data entered. Specifically, I have this code:



    IIf([nest_fail_date] Is Null, Null, IIf(Or([cause_initial_egg_loss_specific] Is Null, [cause_initial_egg_loss_specific] Is Not Null), IIf([cause_subsequent_egg_loss_specific] Is Null, [cause_initial_egg_loss_specific], [cause_subsequent_egg_loss_specific])))

    The error message is "The expression contains invalid syntax. You may have entered a comma without a preceding value or identifier." What am I doing wrong?? Thanks in advance for any input, much appreciated!!

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    IIf(Or([cause_initial_egg_loss_specific] Is Null, [cause_initial_egg_loss_specific] Is Not Null)
    this is excel syntax. In access it would be

    IIf([cause_initial_egg_loss_specific] Is Null OR [cause_initial_egg_loss_specific] Is Not Null,.....

    but not clear why you are testing this anyway, [cause_initial_egg_loss_specific] is either null or not null, it can't be anything else

    perhaps if you explained what you are trying to do, we can provide more focused help

  3. #3
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I think you have confused the OR function syntax with Excel's. In Access, it is just:
    Code:
    IIF(Condition1 OR Condition2,...)

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    First off...

    [cause_initial_egg_loss_specific] Is Null

    isn't valid syntax, in Access VBA...it needs to be

    IsNull([cause_initial_egg_loss_specific])

    Linq ;0)>

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    good point - I'd assumed it was sql, but the error message would have been different

  6. #6
    robinsong is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    2
    Thank you for your responses. I was going off someone else's code and I think there were errors. I played around with it and finally came up with this, which worked to return what I needed in all instances:

    IIf([cause_initial_egg_loss_general] Is Null, Null, IIf([cause_subsequent_egg_loss_specific] Is Not Null, [cause_subsequent_egg_loss_specific], [cause_initial_egg_loss_specific]))

    Thanks again!!

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    keeping the field names short and in English your formula reads

    if initialgeneral is null then return null otherwise if subsequentspecific is not null then return subsequentspecific otherwise return initialspecific

    you could write this more simply as

    iif(initialgeneral is null, null, nz(subsequentspecific,initialspecific))

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

Similar Threads

  1. Trouble with Calculated field.
    By jtm013 in forum Access
    Replies: 12
    Last Post: 02-26-2015, 01:58 PM
  2. Replies: 3
    Last Post: 11-24-2012, 07:33 AM
  3. trouble with one line of code
    By mejia.j88 in forum Programming
    Replies: 3
    Last Post: 01-25-2012, 10:53 PM
  4. VB code in Access '07 trouble
    By Pauldk in forum Reports
    Replies: 2
    Last Post: 02-18-2009, 03:59 PM
  5. Code Trouble?
    By briancb2004 in forum Access
    Replies: 0
    Last Post: 10-08-2008, 04:47 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