Results 1 to 3 of 3
  1. #1
    niklassbordone is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Feb 2018
    Posts
    1

    5 if clauses

    All,



    I have browsed this forum for quite a while now but I couldn't find a similar issue like the one that I am facing. So far this type of expression has never created a problem but right now I am a little bit lost and hope someone can help me:

    I have 5 different columns:
    - Test1
    - Test2
    - Test3
    - Test4
    - Test5

    The first 4 columns (Test 1 - Test 4) are sometimes blank (""). Test 5 always has a data record that is not null.

    I want to merge those columns into one based on the following rule:
    If Test 1 is null => check if Test 2 is null => if yes, check if Test 3 is null => if yes, check if Test4 is null => if yes, enter Test 5, else Test4, then Test3, then Test2 and Test 1

    I used a simple if code
    IIf([TEST_1]="",IIf([TEST_2]="",IIf([TEST_3]="",IIf([TEST_4]="",[TEST_5],[TEST_4]),[TEST_3]),[TEST_2]),[TEST_1])

    But the result is that Test 1 - 4 are consolidated correctly. But as soon as Test 1-4 are null I get to see a blank instead of "Test 5".

    I hope I was able to explain it properly.

    Any clue what my mistake is?

    Thanks
    Niklas

  2. #2
    Beetle is offline Unrelatable
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    Camp Swampy (Denver, CO)
    Posts
    207
    Try the Switch function instead

    SWITCH([TEST_1]<>"",[TEST_1],[TEST_2]<>"",[TEST_2],[TEST_3]<>"",[TEST_3],[TEST_4]<>"",[TEST_4],[TEST_5]<>"",[TEST_5])

    Also, trapping for "" is not the same as trapping for Null. If any of these fields may actually be Null, then you'll want to incorporate the Nz function as well;

    SWITCH(Nz([TEST_1],"")<>"",[TEST_1],............

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    assuming they are nulls, you could just use a nested nz function

    nz(test1,nz(test2,nz(test3,nz(test4,test5))))

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

Similar Threads

  1. Replies: 4
    Last Post: 08-17-2015, 10:04 AM
  2. Creating an SQL query with AND and Or clauses.
    By Johanb26 in forum Queries
    Replies: 2
    Last Post: 07-08-2015, 05:30 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