473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Returning Null Values and Blank Criteria in Form

Hello,
I am trying to get a query that runs off input values in a form in access 2010. I would like to return null values in fields and if a box is not filled out in the form to ignore this parameter. I whave been trying to do this without creating a union query. But this is what I have so far but it keeps saying I have a syntax error and sometime I have gotten you are missing an operator. I know what those errors mean but I am getting a little stuck at the moment. Please help
Expand|Select|Wrap|Line Numbers
  1. SELECT [Tbl-Master List].Group, [Tbl-Master List].User, [Tbl-Master List].[User ID], [tbl-UserEmployeeType].[User Employee Type], [tbl-Company].Company, [tbl-UserLocation].UserLocation, [tbl-UserDepartment].UserDepartment, [tbl-UserTitle].[User Title]
  2. FROM [tbl-UserTitle] INNER JOIN ([tbl-UserDepartment] INNER JOIN ([tbl-UserLocation] INNER JOIN ([tbl-Company] INNER JOIN ([tbl-UserEmployeeType] INNER JOIN [Tbl-Master List] ON [tbl-UserEmployeeType].[User Employee Type] = [Tbl-Master List].[User Employee Type]) ON [tbl-Company].Company = [Tbl-Master List].Company) ON [tbl-UserLocation].UserLocation = [Tbl-Master List].[User Location]) ON [tbl-UserDepartment].UserDepartment = [Tbl-Master List].[User Department]) ON [tbl-UserTitle].[User Title] = [Tbl-Master List].[User Title]
  3. WHERE
  4. (([tbl-UserEmployeeType].[User Employee Type]) Like IIf(IsNull[User Profile Query].[User Employee Type]), _ "*",[Forms]![User Profile Query].[User Employee Type]) AND (([tbl-Company].Company) Like [Forms]![User Profile Query].[Company]) AND (([tbl-UserLocation].UserLocation) Like [Forms]![User Profile Query].[UserLocation]) AND (([tbl-UserDepartment].UserDepartment) Like [Forms]![User Profile Query].[UserDepartment]) AND (([tbl-UserTitle].[User Title]) Like [Forms]![User Profile Query].[User Title])) OR ((([tbl-UserEmployeeType].[User Employee Type]) Is Null) AND (([tbl-Company].Company) Is Null) AND (([tbl-UserLocation].UserLocation) Is Null) AND (([tbl-UserDepartment].UserDepartment) Is Null) AND (([tbl-UserTitle].[User Title]) Is Null));
  5.  
Sep 26 '12 #1

✓ answered by Seth Schrock

Oops. I found a mistake in my previous post.

Expand|Select|Wrap|Line Numbers
  1. WHERE IIF(Not IsNull(Forms![User Profile Query]![User Employee Type]), [tbl-Master List].[User Employee Type] Like Forms![User Profile Query]![User Employee Type], '*')
  2. AND IIF(Not IsNull([Forms]![User Profile Query].[Company]), [tbl-Master List].Company Like [Forms]![User Profile Query].[Company], '*')
  3. AND IIF(Not IsNull([Forms]![User Profile Query].[UserLocation]), [tbl-Master List].UserLocation Like [Forms]![User Profile Query].[UserLocation], '*')
  4. AND IIF(Not IsNull([Forms]![User Profile Query].[UserDepartment]), [tbl-Master List].UserDepartment Like [Forms]![User Profile Query].[UserDepartment], '*')
  5. AND IIF(Not IsNull([Forms]![User Profile Query].[User Title]), [tbl-Master List].[User Title] Like [Forms]![User Profile Query].[User Title], '*')

24 3142
Seth Schrock
2,965 Expert 2GB
For anyone else working on this, here is the code in a nicer format:

Expand|Select|Wrap|Line Numbers
  1. SELECT [Tbl-Master List].Group
  2. , [Tbl-Master List].User
  3. , [Tbl-Master List].[User ID]
  4. , [tbl-UserEmployeeType].[User Employee Type]
  5. , [tbl-Company].Company
  6. , [tbl-UserLocation].UserLocation
  7. , [tbl-UserDepartment].UserDepartment
  8. , [tbl-UserTitle].[User Title]
  9. FROM [tbl-UserTitle] 
  10. INNER JOIN ([tbl-UserDepartment] 
  11. INNER JOIN ([tbl-UserLocation] 
  12. INNER JOIN ([tbl-Company] 
  13. INNER JOIN ([tbl-UserEmployeeType] 
  14. INNER JOIN [Tbl-Master List] 
  15. ON [tbl-UserEmployeeType].[User Employee Type] = [Tbl-Master List].[User Employee Type]) 
  16. ON [tbl-Company].Company = [Tbl-Master List].Company) 
  17. ON [tbl-UserLocation].UserLocation = [Tbl-Master List].[User Location]) 
  18. ON [tbl-UserDepartment].UserDepartment = [Tbl-Master List].[User Department]) 
  19. ON [tbl-UserTitle].[User Title] = [Tbl-Master List].[User Title]
  20. WHERE
  21. (([tbl-UserEmployeeType].[User Employee Type]) Like 
  22.     IIf(IsNull[User Profile Query].[User Employee Type])
  23.         , _ "*"
  24.         ,[Forms]![User Profile Query].[User Employee Type]) 
  25. AND (([tbl-Company].Company) Like [Forms]![User Profile Query].[Company]) 
  26. AND (([tbl-UserLocation].UserLocation) Like [Forms]![User Profile Query].[UserLocation]) 
  27. AND (([tbl-UserDepartment].UserDepartment) Like [Forms]![User Profile Query].[UserDepartment]) 
  28. AND (([tbl-UserTitle].[User Title]) Like [Forms]![User Profile Query].[User Title])) 
  29. OR ((([tbl-UserEmployeeType].[User Employee Type]) Is Null) 
  30. AND (([tbl-Company].Company) Is Null) 
  31. AND (([tbl-UserLocation].UserLocation) Is Null) 
  32. AND (([tbl-UserDepartment].UserDepartment) Is Null) 
  33. AND (([tbl-UserTitle].[User Title]) Is Null));
Trey, I am working on this, but don't have an answer yet.
Sep 26 '12 #2
Seth Schrock
2,965 Expert 2GB
Trey, on line 22 of the code that I posted, try removing the ending parenthesis at the end of the line: ) See what you get now.

Also, in lines 29 through 33, try making the criteria be

Expand|Select|Wrap|Line Numbers
  1. AND (IsNull([tbl-UserEmployeeType].[User Employee Type]))
Sep 26 '12 #3
K i changed the code in the first where clause to test and it will query now but if I leave a form field blank i get an error that says This is typed incorrectly, or it is too complex to be evaluated. It will run if all form fields are fillled out.

Expand|Select|Wrap|Line Numbers
  1. WHERE ((([tbl-UserEmployeeType].[User Employee Type]) Like IIf(IsNull([Forms]![User Profile Query].[UserEmployeeType]),"_"*"",[Forms]![User Profile Query].[UserEmployeeType])) AND (([tbl-Company].Company) Like [Forms]![User Profile Query].[Company]) AND (([tbl-UserLocation].UserLocation) Like [Forms]![User Profile Query].[UserLocation]) AND (([tbl-UserDepartment].UserDepartment) Like [Forms]![User Profile Query].[UserDepartment]) AND (([tbl-UserTitle].[User Title]) Like [Forms]![User Profile Query].[User Title]));
Sep 26 '12 #4
Seth Schrock
2,965 Expert 2GB
Try making the criteria in lines 29 through 33 be:

Expand|Select|Wrap|Line Numbers
  1. AND (IsNull([tbl-UserEmployeeType].[User Employee Type]))
I added this to my previous post, but I think that you were probably typing at the same time as me.
Sep 26 '12 #5
Seth Schrock
2,965 Expert 2GB
Upon further analysis of your code, I think that your problem may be because your Joins are some of the same fields as your criteria. Thus, if one of your criteria fields are blank, then your Join field is also blank. I'm not an expert at how the Joins work, but I'm guessing that this would cause problems.
Sep 26 '12 #6
I got the same error about the query being too complex
Sep 26 '12 #7
Seth Schrock
2,965 Expert 2GB
What are you trying to do? I might be able to come up with a way to get the functionality that you are looking for without having to do this query.
Sep 26 '12 #8
So I was thinking about adding a primary key like user id to these tables and making these joins, do you think that would work?
Sep 26 '12 #9
I am trying to pull data out through a form that runs a query, but I want to be able to leave a form field blank where it will ignore the parameter or this form field
Sep 26 '12 #10
Seth Schrock
2,965 Expert 2GB
It is hard to say without seeing your database, but I have found that changing the PK of a table envolves a bunch of work and usually isn't needed if the database was designed correctly to begin with.

What is the purpose of pulling data out through a form? In other words, what does the end user see? End users usually have no idea what a query is or that they are getting data from a query.
Sep 26 '12 #11
This is going to be used with the IT department to give user provisioning rights. So when a new employee comes in they fill out areas on a form for department, employment status, etc to pull out the access rights for that specific user and the groups they need to be imported into AD. I didn't create this database they came to me after it was built and said this query doesn't work. So I am thinking I might have to make changes to how it was created to make it work correctly. Sometime they have null values in their personell record or need to leave employeement status or something else blank to pull a new employee out.
Sep 26 '12 #12
Seth Schrock
2,965 Expert 2GB
Okay. That makes more sense. So I'm guessing that you have a main table (possibly tbl-Master List) that has multiple tables related directly to it (for example, tbl-UserDepartment, tbl-UserLocation, etc.) Is this correct?
Sep 26 '12 #13
Yes sir, then she designed the form fields drop down box to pull from a query in the smaller tables
Sep 26 '12 #14
Seth Schrock
2,965 Expert 2GB
Okay, then you should be able to run the query without all the joins because you'll actually have your criteria matching the values in [tbl-Master List]. Like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT [Tbl-Master List].Group
  2. , [Tbl-Master List].User
  3. , [Tbl-Master List].[User ID]
  4. , [tbl-UserEmployeeType].[User Employee Type]
  5. , [tbl-Company].Company
  6. , [tbl-UserLocation].UserLocation
  7. , [tbl-UserDepartment].UserDepartment
  8. , [tbl-UserTitle].[User Title]
  9. FROM [tbl-Master List]
  10. WHERE (([tbl-Master List].[User Employee Type]) Like 
  11.     IIf(IsNull[User Profile Query].[User Employee Type]
  12.         , _ "*"
  13.         ,[Forms]![User Profile Query].[User Employee Type]) 
  14. AND (([tbl-Master List].Company) Like [Forms]![User Profile Query].[Company]) 
  15. AND (([tbl-Master List].UserLocation) Like [Forms]![User Profile Query].[UserLocation]) 
  16. AND (([tbl-Master List].UserDepartment) Like [Forms]![User Profile Query].[UserDepartment]) 
  17. AND (([tbl-Master List].[User Title]) Like [Forms]![User Profile Query].[User Title])) 
  18. OR ((([tbl-Master List].[User Employee Type]) Is Null) 
  19. AND (([tbl-Master List].Company) Is Null) 
  20. AND (([tbl-Master List].UserLocation) Is Null) 
  21. AND (([tbl-Master List].UserDepartment) Is Null) 
  22. AND (([tbl-Master List].[User Title]) Is Null));[tbl
If this doesn't work, then I have one more idea. It takes more coding so I'll wait to hear back.
Sep 26 '12 #15
I tried the code and changed a little syntax to run it. After I ran it it pulled up parameter boxes, so I input them with the data to test to see if it would pull out any data. I made all equal without leaving any blank to test it before I left a field blank. I still got the same error saying that it was too complex. I tried earlier in a test DB making the 5 other tables have a common key and puling the data out but even distinctly I was running too large a query to retrieve data. I tried to link all the tables back to the one key userID but with no luck. I am stumped. Maybe a night away from it and I will come in with fresh ideas. I appreciate your help on this issue.
Sep 26 '12 #16
Seth Schrock
2,965 Expert 2GB
Lets try to narrow it down. First, try the query without the WHERE clause. If that works (I'm almost positive that it will), then try adding the WHERE clause up to and including line 17. Make sure each field has something in it and then run the query. Let me know what the results are.
Sep 26 '12 #17
zmbd
5,501 Expert Mod 4TB
Spaces and dashes in field names can cause issues... even when enclosed within square brackets, (so can reserved words). I've had databases choke on such things with "too complex" errors that were often resolved by renaming fields. (I inherit a lot of broken databases - yea me)

You really should consider removing the spaces in all field names and converting dashes to underscores.

It is still considered best practice not to use anything except alphanumeric characters and the underscore when naming fields, files, directories etc...
Sep 26 '12 #18
K without the where clause it does work, when data is inputed into the parameter, I took out all the other where clauses except the Iif statement and I got the same error. I am going to try and change up the Iif(isnull statement) to see if I can get it to work without all the other parameters.
Sep 27 '12 #19
Seth Schrock
2,965 Expert 2GB
Here is my other option. Try this in the WHERE clause:

Expand|Select|Wrap|Line Numbers
  1. WHERE IIF(Not IsNull(Forms![User Profile Query]![User Employee Type]), [tbl-Master List].[User Employee Type] Like Forms![User Profile Query]![User Employee Type], '*')
  2. AND IIF(Not IsNull([Forms]![User Profile Query].[Company]), [tbl-Master List].Company Like [Forms]![User Profile Query].[Company], '*')
  3. AND IIF(Not IsNull([Forms]![User Profile Query].[UserLocation]), [tbl-Master List].UserLocation Like [Forms]![User Profile Query].[UserLocation], '*')
  4. AND IIF(Not IsNull([Forms]![User Profile Query].[UserDepartment]), [tbl-Master List].UserDepartment Like [Forms]![User Profile Query].[UserDepartment], '*')
  5. AND IIF(Not IsNull([tbl-Master List].[User Title]), [tbl-Master List].[User Title] Like [Forms]![User Profile Query].[User Title], '*')
You should be able to copy and paste this directly into your query.
Sep 27 '12 #20
Seth Schrock
2,965 Expert 2GB
Oops. I found a mistake in my previous post.

Expand|Select|Wrap|Line Numbers
  1. WHERE IIF(Not IsNull(Forms![User Profile Query]![User Employee Type]), [tbl-Master List].[User Employee Type] Like Forms![User Profile Query]![User Employee Type], '*')
  2. AND IIF(Not IsNull([Forms]![User Profile Query].[Company]), [tbl-Master List].Company Like [Forms]![User Profile Query].[Company], '*')
  3. AND IIF(Not IsNull([Forms]![User Profile Query].[UserLocation]), [tbl-Master List].UserLocation Like [Forms]![User Profile Query].[UserLocation], '*')
  4. AND IIF(Not IsNull([Forms]![User Profile Query].[UserDepartment]), [tbl-Master List].UserDepartment Like [Forms]![User Profile Query].[UserDepartment], '*')
  5. AND IIF(Not IsNull([Forms]![User Profile Query].[User Title]), [tbl-Master List].[User Title] Like [Forms]![User Profile Query].[User Title], '*')
Sep 27 '12 #21
This code works if all parameters are filled in but if I leave one blank it returns no data. I tried to leave user employee type blank and it did not error this time but it returned no data. I am going to test to see if it returns people that have null values in these fields.
Sep 27 '12 #22
I think you might have it right, i will let you know as soon as i get through testing
Sep 27 '12 #23
It works, thanks you so much for your help, the only reason I didn't think it worked that last time was because there is another query that counts and pulls out data equal to another table but I think I can get that one going. I really appreciate your help you are a life saver.
Sep 27 '12 #24
Seth Schrock
2,965 Expert 2GB
Not a problem. Glad I was finally able to get it to work for you. Please select the post that best helped you and select it as best answer.
Sep 27 '12 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mark Davenport | last post by:
Hi, Here's my question: How do I pass a NULL value in a variable to a MySQL DB? Here's an overview of my problem: I have a PHP page that processes code, then inserts the code into a database....
1
by: Jamie Burns | last post by:
Hello, I am trying to perform a LEFT JOIN on a table which may or may not have matching rows. If there are no matching rows, it returns NULL's for all the missing fields. Is there anyway of...
1
by: Scott | last post by:
In the result of a SELECT statement, how can you change cell values from NULL to BLANK? The following does NOT do it: SET fieldname = ' ' WHERE fieldname IS NULL Also, for colums with a DATE...
6
by: AAVF | last post by:
Hi We have a problem with a query. An Access database links via ODBC to a UNIX server. To speed things, we use the ODBC to load the relevant tables to the local PC that runs Access so that...
0
by: Farooque Khan | last post by:
Hi, I have a web service which returns values by reference and also returns array lists in some cases. I am calling this from a VC 7 application. The problem is that if ANY of the values...
1
by: RDizzle | last post by:
okay. so all i am doing is changing a registration script that uses $_GET to a script that uses $_POST, but the validation script now returns NULL values for all posted vars. What's the deal? ...
5
by: bill | last post by:
I am a relative newbie to PHP, but not to other languages. I wrote a test script to upload files. In the php section above the page I get the expected content of the $_FILES array. When I have...
9
by: GotDotNet? | last post by:
I have a dataset and I have to loop through it and some of the values for an insertition into the db. Some of the fields are integers and booleans but contain a NULL in the field. how can I...
2
by: KMEscherich | last post by:
Microsoft Access '97 Main Form and 3 subforms. Code = VBA Hi there am wondering how I would be able to do the following: I need to ensure the end-user makes an entry in controls on both the...
1
by: zufie | last post by:
Hi, How do I set the Criteria in a query for my City field to return those records that have nothing/no value/null value that is, no city name entered. City field used Text data type. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.