Solved

Assigning a value if IIF IsNull is not true.

Posted on 2014-01-21
7
569 Views
Last Modified: 2014-01-30
Hello Experts.

I have what I hope is a simple question.  I have some fields that I would like to add together in a query.  Some of the fields may contain null values.  I would like any field with a null value to have a value of 0, and all other fields to be assigned a value of 1.

For example:

fld1 = null therefore the value assigned would be 0
fld2 = 4 therefore the value assigned would be 1
fld3 = 2 therefore the value assigned would be 1
fld4 = 7 therefore the value assigned would be 1

The desired result of the formula is 3.

I have tried the following, but I get 13 (4+2+7) instead of 3 (1+1+1).

Sum(IIF(Not IsNull([fld1]),1,0)+IIf(Not IsNull([fld2]),1,0)+IIF(Not IsNull([fld3]),1,0)+
IIF(Not IsNull([fld4]),1,0)

Any help would be greatly appreciated.  

I am currently using MS Access 2013 on Windows 7.

Thanks,
J
0
Comment
Question by:ferguson_jerald
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39797336
Try this expression:

YourField:  ABS((NOT IsNull(fld1))  + (NOT IsNull(fld2)) + (NOT IsNull(fld3)) + (NOT IsNull(fld4)))
0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 39797337
Try:

SUM(-(NZ([fld1],0) > 0)-(NZ([fld2],0) > 0) - (NZ([fld3],0)>0) - (NZ([fld4],0)>0))

This basically creates boolean values (0 or -1) for each field using the >0 comparison
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39797366
<<
I have tried the following, but I get 13 (4+2+7) instead of 3 (1+1+1).
>>

From your description, it sounds like you are simply trying to add across columns (ie: horizontal sum).

The SUM function gets a total over all of the records in your table (vertical sum)
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 37

Expert Comment

by:PatHartman
ID: 39797377
Your expression looks correct although people have less trouble understanding positive statements than negative ones so I would change it to:

Sum(IIF(IsNull([fld1]),0,1)+IIf(IsNull([fld2]),0,1)+IIF(IsNull([fld3]),0,1)+
IIF(IsNull([fld4]),0,1))
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 39797414
This should do:

YourField:  Abs([fld1] Is Not Null) + Abs([fld2] Is Not Null) + Abs([fld3] Is Not Null) + Abs([fld4] Is Not Null)

/gustav
0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 39797426
Please disregard my reply.  I just realized you indicated NULL = 0, any other value (including 0) = 1).  My response assumed that zeros were to be counted as zero as well.

How about:

(1-IsNull([fld1])) + (1-IsNull([fld2])) + ((1-IsNull([fld3])) + (1-IsNull([fld4]))
0
 
LVL 10

Accepted Solution

by:
Gozreh earned 500 total points
ID: 39820888
Just remove the "Sum"

So now you can use
Or:
IIf(Not IsNull([fld1]),1,0)+IIf(Not IsNull([fld2]),1,0)+IIf(Not IsNull([fld3]),1,0)+IIf(Not IsNull([fld4]),1,0)
Or:
ABS((NOT IsNull(fld1))  + (NOT IsNull(fld2)) + (NOT IsNull(fld3)) + (NOT IsNull(fld4)))
Or:
Abs([fld1] Is Not Null)+Abs([fld2] Is Not Null)+Abs([fld3] Is Not Null)+Abs([fld4] Is Not Null)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question