Results 1 to 14 of 14
  1. #1
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181

    Query criteria with values and null

    Hi, I'm stuck trying to make a query and its giving me the run around.

    The query is made and I'm trying to add a criteria to a column. Not all the records have a value in the column and I want those to be part of the criteria.

    I have this and it works

    <>"on road" And <>"weighed" And <>"review"

    of course with that criteria the records with no value disappear. If I add "And Is Null" to the above criteria it comes up with no records all together.

    If I add just "is null" it shows only the records with no value in the column as it should but for the life of me I can't mix the 2.



    So how do I say

    <>"on road" And <>"weighed" And <>"review" And Is Null

    Thanks

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    You could try OR instead of AND:
    <>"on road" OR <>"weighed" OR <>"review" OR Is Null
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    Thanks, I tried that already but it returns everything even if the value is "on road" or whatever.

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Can you post the query's SQL
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    This is what I'm using.

    Code:
    SELECT Vendors.VendorID, [Firstname]+" "+[Lastname] AS FullName, Vendors.LastName, VendorsLastStatus.LastOfStatus
    FROM Vendors LEFT JOIN VendorsLastStatus ON Vendors.VendorID = VendorsLastStatus.VendorID
    GROUP BY Vendors.VendorID, [Firstname]+" "+[Lastname], Vendors.LastName, VendorsLastStatus.LastOfStatus
    HAVING (((Vendors.LastName)<>"temp storage") AND ((VendorsLastStatus.LastOfStatus)<>"on road" And (VendorsLastStatus.LastOfStatus)<>"weighed" And (VendorsLastStatus.LastOfStatus)<>"review"))
    ORDER BY [Firstname]+" "+[Lastname];

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Try Is Null in the second criteria line, in each of the columns that already have criteria set.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    If I put Is Null on 2nd line for both columns with criteria it changes nothing, if I only add it to the column I'm having trouble with it shows the correct records however it ignores the criteria on the other column so I get a bunch of records I don't want.

  8. #8
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Did you try something like below :
    Code:
     
    GROUP BY 
    ...............
    HAVING 
    	(
    		(
    			(Vendors.LastName)<>"temp storage"
    		) 
    		AND 
    		(
    			(VendorsLastStatus.LastOfStatus)<>"on road" 
    			And 
    			(VendorsLastStatus.LastOfStatus)<>"weighed" 
    			And 
    			(VendorsLastStatus.LastOfStatus)<>"review" 
    			Or 
    			(VendorsLastStatus.LastOfStatus) Is Null
    		)
    	)
    ORDER BY 
    ...............
    Thanks

  9. #9
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    Nice, that works, thanks! That put <>"temp storage" on both criteria lines and what Bob said to do one the other criteria.

    Thanks for the help!

  10. #10
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Just check, what happens if you have Null Values in LastName field in the table.
    Then check out below :

    Code:
    SELECT 
    	Vendors.VendorID, 
    	[Firstname]+" "+Nz([Lastname],"") AS FullName, 
    	Vendors.LastName, VendorsLastStatus.LastOfStatus
    FROM 
    	Vendors 
    	LEFT JOIN 
    	VendorsLastStatus 
    	ON 
    	Vendors.VendorID = VendorsLastStatus.VendorID
    GROUP BY 
    	Vendors.VendorID, 
    	[Firstname]+" "+Nz([Lastname],""), 
    	Vendors.LastName, 
    	VendorsLastStatus.LastOfStatus
    HAVING 
    	(
    		(
    			(Vendors.LastName)<>"temp storage"
    		) 
    		AND 
    		(
    			(VendorsLastStatus.LastOfStatus)<>"on road" 
    			And 
    			(VendorsLastStatus.LastOfStatus)<>"weighed" 
    			And 
    			(VendorsLastStatus.LastOfStatus)<>"review"
    		)
    	) 
    	OR 
    	(
    		(
    			(Vendors.LastName)<>"temp storage"
    		) 
    		AND 
    		(
    			(VendorsLastStatus.LastOfStatus) Is Null
    		)
    	) 
    	OR 
    	(
    		(
    			(Vendors.LastName) Is Null
    		)
    	)
    ORDER BY [Firstname]+" "+Nz([Lastname],"");
    Thanks

  11. #11
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    That should never happen, the table requires a last name but I changed it to that just in case that changes later. It works flawless. Thanks!!

  12. #12
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Glad you found it helpful. I think we should avoid Null Values & instead have default values set everywhere.
    The credit is entirely Bob's. I was just translating his thoughts as he was thinking.

    Thanks

  13. #13
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    I try hard to avoid the null values but in this case its unavoidable as I'm mixing tables for the query and new vendors don't have an order in the system yet so no status stored anywhere. I tried the Nz thing hoping it will fill it for the query sake but it did nothing, I use that method for lots of number columns. Anyways thanks again and yes Bob was helpful as always.

    Cheers

  14. #14
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Glad your problem is sorted. Always happy to help, if I can
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. Replies: 1
    Last Post: 02-23-2012, 02:27 PM
  2. Replies: 12
    Last Post: 12-11-2011, 05:04 PM
  3. Query not returning null values
    By janelgirl in forum Access
    Replies: 5
    Last Post: 10-11-2011, 10:31 AM
  4. Query with null values
    By Psyclone in forum Queries
    Replies: 3
    Last Post: 03-30-2011, 09:57 PM
  5. Null Values in query
    By LesleaOH in forum Queries
    Replies: 0
    Last Post: 10-19-2009, 04:45 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