Avatar of Ernest Grogg
Ernest Grogg

asked on 

IIF to include Null Values from Table

Hello,

I can't seem to get the statement for my query....

IIF(IsNull  Like "*" & Forms!frmForm!myfield & "*"


I am normally using the Like statement across the board but I want to make sure that if the field in the table is Null or empty I want to still return all the records for that field.  If there is a null field in the table it leaves that record out of the query....I want to make sure I include that record.


Don't know what to parts for the true and false would be?
Microsoft AccessSQL

Avatar of undefined
Last Comment
Mike Eghtebas
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

IIF(Nz(FieldName,'')<>'',"FieldInNotNull", "FieldIsNull")

I am not able to follow what your are trying to accomplish, but I detect empty space or null value like above.

Here:
Nz(FieldName,'') return '' if the it is '' or null

Then it compares <> ''   if true then it has a value otherwise it is either '' or null.

Mike
Avatar of Kelvin Sparks
Kelvin Sparks
Flag of New Zealand image

I think it needs to be

IIF(your field LIKE .... OR isNULL(your field),List what you want, the rest)

Kelvin
Avatar of Ernest Grogg
Ernest Grogg

ASKER

What I have is a SQL:

SELECT MGNameAddressPhone.FName, MGNameAddressPhone.LName, MGNameAddressPhone.Gender, MGNameAddressPhone.SSN
FROM MGNameAddressPhone
WHERE (((MGNameAddressPhone.FName) Like "*" & [Forms]![frmRecordsEdit]![FName] & "*") AND ((MGNameAddressPhone.LName) Like "*" & [Forms]![frmRecordsEdit]![LName] & "*") AND ((MGNameAddressPhone.Gender) Like "*" & [Forms]![frmRecordsEdit]![Gender] & "*") AND ((MGNameAddressPhone.SSN) Like "*" & [Forms]![frmRecordsEdit]![Gender] & "*"));

Open in new window



What happens if is the field:  SSN has any null values the Like Statement doesn't include that in the list.  it removes those that have null values in the table

What I have is say 34 records in the Table

15 records with a first name of "TEST"  but out of those 15, 5 have SSN's listed...so only 5 records show up in the query....I need all 15 to show in the results if I am searching on "TEST" for the First Name....
Avatar of Kelvin Sparks
Kelvin Sparks
Flag of New Zealand image

Try changing the ANDS to ORs
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

based on my understanding, is this what you trying to achieve?

SELECT MGNameAddressPhone.FName, MGNameAddressPhone.LName, MGNameAddressPhone.Gender, MGNameAddressPhone.SSN
FROM MGNameAddressPhone
WHERE (
((MGNameAddressPhone.FName) Like "*" & [Forms]![frmRecordsEdit]![FName] & "*") AND ((MGNameAddressPhone.LName) Like "*" & [Forms]![frmRecordsEdit]![LName] & "*") AND ((MGNameAddressPhone.Gender) Like "*" & [Forms]![frmRecordsEdit]![Gender] & "*") AND 

(
  ((MGNameAddressPhone.SSN) Like "*" & [Forms]![frmRecordsEdit]![Gender] & "*") OR       (MGNameAddressPhone.SSN IS NULL)
)

);

Open in new window

Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

BTW, I need to explain something about my earlier statement "And in English writing translates to OR in SQL."

This applies sometimes. Not necessarily always.

Example when it applies.
Requirement: Get Address for Jack and Bob from Employees table:
Select Address From Employees
Where Name = 'Jack' or Name ='Bob'

Example When doesn't apply.
I don't know yet. I have to investigate.

Mike
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo