Solved

Access Query - Null in where statement issue

Posted on 2014-07-30
4
355 Views
Last Modified: 2014-08-01
Hi,

When I run the following query the results does not return rows where CHARGE is null. Instead it returns rows where CHARGE is anything but null.

 SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE (((HLabor.CHARGE) Is Null) AND ((HLabor.DECLINED)=False) AND ((HLabor.CATAGORY)="00") AND ((HLabor.LABOR_OP)="001")) OR (((HLabor.CATAGORY)="") AND ((HLabor.LABOR_OP)=""));

Open in new window


I run the same query without the additional criteria and it returns just the rows where CHARGE is = null:

SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE (((HLabor.CHARGE) Is Null));

Open in new window


Why is the first query ignoring the request for rows where CHARGE is null in addition to the other criteria? Is there a workaround?
0
Comment
Question by:ckelsoe
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 40230718
Have a look at your usage of brackets (parenthesis):
SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE (((HLabor.CHARGE) Is Null) AND ((HLabor.DECLINED)=False) AND ((HLabor.CATAGORY)="00") AND ((HLabor.LABOR_OP)="001")) OR (((HLabor.CATAGORY)="") AND ((HLabor.LABOR_OP)=""));

Open in new window

If we take away the extra brackets Access has inserted, the query can be simplified to this:
SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE (
        (HLabor.CHARGE Is Null) AND (HLabor.DECLINED=False) AND (HLabor.CATAGORY="00") AND (HLabor.LABOR_OP="001")
      ) 
      OR 
      (
        (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")
      );

Open in new window

have a look what I have done. I have made the where clause more readable by separating two big criteria. Now, as you can see the criteria where you want the Charge IS NULL also includes these:
(HLabor.CHARGE Is Null) AND (HLabor.DECLINED=False) AND (HLabor.CATAGORY="00") AND (HLabor.LABOR_OP="001")

Open in new window

However, the second criteria is also asking to select additional rows for
 (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")

Open in new window

See, how these additional rows do not request the Charge Is NULL. If you want these additional rows to also satisfy the condition for  Charge Is NULL you either include it here:
(HLabor.CHARGE Is Null) AND (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")

Open in new window

or, even better, reformat your where clause like this:
SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE (HLabor.CHARGE Is Null) AND 
      (
         (
            (HLabor.DECLINED=False) AND (HLabor.CATAGORY="00") AND (HLabor.LABOR_OP="001")
         ) 
         OR 
         (
            (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")
         )
      );

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 40230956
This applies the Is Null condition on both sides of the OR operator:
SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, 
HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE 
(((HLabor.CHARGE) Is Null) 
AND ((HLabor.DECLINED)=False) 
AND ((HLabor.CATAGORY)="00") 
AND ((HLabor.LABOR_OP)="001")) 

OR 
(((HLabor.CHARGE) Is Null) AND ((HLabor.CATAGORY)="") AND ((HLabor.LABOR_OP)=""));

Open in new window

0
 
LVL 24

Expert Comment

by:chaau
ID: 40230961
Yes, it does. This is what I proposed as a first option:
If you want these additional rows to also satisfy the condition for  Charge Is NULL you either include it here:
(HLabor.CHARGE Is Null) AND (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")
However, you can make it more readable if you use the second option I have proposed:
WHERE (HLabor.CHARGE Is Null) AND 
      (
         (
            (HLabor.DECLINED=False) AND (HLabor.CATAGORY="00") AND (HLabor.LABOR_OP="001")
         ) 
         OR 
         (
            (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")
         )
      );

Open in new window

0
 

Author Closing Comment

by:ckelsoe
ID: 40235571
Thanks - that worked perfect.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now