473,503 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple parameter query; ignore blanks & have results match all parameters entered

10 New Member
I am using Access 2007 to design a database that will allow users to search an inventory of historical photographs. Currently I have a table with fields such as Date, ID, Description, Project, Division, Unit, Facility, Feature, Subfeature, Region, Area Office, and State. I am using a query by form method that have either text boxes or combo boxes for each of the fields. I want users to be able to enter in the fields that they want to search by and leave others blank. I want the query to disregard the parameters left blank and then display the results that match ALL the parameters entered. For example I want to be able to enter a date range, Project Name, and Region and want the results to match all three criteria not all the records within the date range plus all records associated with the project plus all records from the specified region. I think it may have something to do with the use of AND and OR. If anyone has any words of wisdom it would be greatly appreciated. This is what I have come up with so far:

Expand|Select|Wrap|Line Numbers
  1. SELECT [ID]
  2.       ,[Project]
  3.       ,[Division]
  4.       ,[Unit]
  5.       ,[Feature]
  6.       ,[Subfeature]
  7.       ,[Region]
  8.       ,[Spec No]
  9.       ,[State]
  10.       ,[Date]
  11.       ,[Description]
  12.       ,[Photographer]
  13.       ,[Field1]
  14.       ,[Latitude]
  15.       ,[Longitude]
  16.       ,[Facility]
  17.       ,[AreaOffice]
  18.  
  19. FROM   [Historical Photos]
  20.  
  21. WHERE  (([Project]=[Forms]![Search]![txtProject]) OR ([Forms]![Search]![txtProject] Is Null)
  22.   AND  ([Division]=[Forms]![Search]![txtDivision]) OR ([Forms]![Search]![txtDivision] Is Null)
  23.   AND  ([Unit]=[Forms]![Search]![txtUnit]) OR ([Forms]![Search]![txtUnit] Is Null)
  24.   AND  ([ID]=[Forms]![Search]![txtPhotoID]) OR ([Forms]![Search]![txtPhotoID] Is Null)
  25.   AND  ([Feature]=[Forms]![Search]![txtFeature]) OR ([Forms]![Search]![txtFeature] Is Null)
  26.   AND  ([Subfeature]=[Forms]![Search]![txtSubfeature]) OR ([Forms]![Search]![txtSubfeature] Is Null)
  27.   AND  ([Facility]=[Forms]![Search]![txtFacility]) OR ([Forms]![Search]![txtFacility] Is Null)
  28.   AND  ([State]=[Forms]![Search]![cboState]) OR ([Forms]![Search]![cboState] Is Null)
  29.   AND  ([Region]=[Forms]![Search]![cboRegion]) OR ([Forms]![Search]![cboRegion] Is Null)
  30.   AND  ([AreaOffice]=[Forms]![Search]![cboAreaOffice]) OR ([Forms]![Search]![cboAreaOffice] Is Null)
  31.   AND  ([Description] Like "*" & [Forms]![Search]![txtDescription] & "*") OR ([Forms]![Search]![txtDescription] Is Null)
  32.   AND  ([Date] Between [Forms]![Search]![txtStart] AND [Forms]![Search]![txtEnd]) OR ([Forms]![Search]![txtStart] Is Null OR [Forms]![Search]![txtEnd] Is Null));
Jul 13 '10 #1
21 8287
colintis
255 Contributor
Try using LIKE and Nz function in your query like this, I'll only take your first parameter and the date parameter for the example:
Expand|Select|Wrap|Line Numbers
  1. WHERE (([Historical Photos].Project) LIKE '" & Nz([Forms]![Search]![txtProject],"*") & "')
  2. AND (([Historical Photos].Date) >= LIKE '# & Nz([Forms]![Search]![txtStart],"*") & #')
  3. AND (([Historical Photos].Date) <= LIKE '# & Nz([Forms]![Search]![txtEnd],"*" & #')
Take notice on the quotes, the [" &] and [& "] is used for strings. The date I would try with this method as I haven't tried using the BETWEEN function with LIKE. And also the brackets, make sure they are closed to the correct ones.

Reference for Nz function:
http://www.techonthenet.com/access/f...dvanced/nz.php
Finally, quote the codes to the CODE TAG.
Jul 13 '10 #2
katlee
10 New Member
I used what you gave me and this error message "Syntax error (missing operator) in query expression" came up and it highlighted LIKE.
Jul 13 '10 #3
katlee
10 New Member
Also I tried
Expand|Select|Wrap|Line Numbers
  1. WHERE ((([Historical Photos].Project) Like '" & Nz([Forms]![Search]![txtProject],"*") & "') AND (([Historical Photos].Division) Like '" & Nz([Forms]![Search]![txtDivision],"*") & "'));
as a test because there was no syntax error and the query returned zero results when entering just one parameter and zero results when entering both parameters.
Jul 13 '10 #4
colintis
255 Contributor
@katlee
Can you try removing the --> ,"*" <-- in the Nz function? the "*" may be identified as a value instead of a null.

Or in a more complicate method is to create a filter condition statement in VBA. using something like this with DAO:
Expand|Select|Wrap|Line Numbers
  1. Dim firstCond As Boolean = False
  2. Dim strSQL As String
  3. strSQL = "SELECT [blah blah blah] " & _
  4.          "FROM [Historical Photos] " & _
  5.          "WHERE "
  6. If Not IsNull([Forms]![Search]![txtProject]) Then
  7.     strSQL = strSQL & "[Historical Photos].Project = [Forms]![Search]![txtProject"
  8. End If
  9.  
Jul 14 '10 #5
katlee
10 New Member
I tried removing "*" and the query still didn't return any results.

I thought there may be a way to do it using VBA but I have never done anything with VBA and am a little intimidated. Would I write this code in the "On Click" event of a button on the form where the users enter the parameters?

Right now I have a form thats record source is the query and am using a macro to get it to pop up and display the results. Is there a way to get this same effect using VBA?

Also, would I use what I had originally in the WHERE or use your suggestion (Like Nz)...
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT [blah blah blah] " & _ 
  2.          "FROM [Historical Photos] " & _ 
  3.          "WHERE " (what goes here?)
Thank you so much for your time!!!!
Jul 14 '10 #6
colintis
255 Contributor
@katlee
Yes you can do that on_click event in VBA, if you want more basic reference on how to program VBA, you can click THIS LINK.

The method of working out your filter criterias have 2 methods, one is by ADO, and one is by DAO. Both works the same to connect a table, what different is the way to store the data into the table. The one I wrote above was using a DAO method, when there's a true to one of the IF criteria, it will append the filter into the SQL WHERE clause.

For the click event in VBA code with your requirements, it would be roughly in these steps to proceed.
=======================================
  1. Declare the variable to store strings, connections, etc
  2. Connect to the database table
  3. run through the if-else statements to append addition criterias into the SQL string
  4. Start executing the SQL
  5. when the things are complete, use MsgBox to create pop-up message.
Jul 15 '10 #7
NeoPa
32,557 Recognized Expert Moderator MVP
Colintis is broadly leading you along the right lines here, but it will always be complicated working with such a full set of SQL. I would suggest simplifying the problem, until the fundamental points are understood, then maybe expanding that new understanding to rebuild the full query. Let's start then with :
Expand|Select|Wrap|Line Numbers
  1. SELECT [ID]
  2.       ,[Project]
  3.       ,[Date]
  4.       ,[Description]
  5.  
  6. FROM   [Historical Photos]
  7.  
  8. WHERE  (([ID]=[Forms]![Search]![txtPhotoID]) OR ([Forms]![Search]![txtPhotoID] Is Null)
  9.   AND  ([Project]=[Forms]![Search]![txtProject]) OR ([Forms]![Search]![txtProject] Is Null)
  10.   AND  ([Date] Between [Forms]![Search]![txtStart] And [Forms]![Search]![txtEnd]) OR ([Forms]![Search]![txtStart] Is Null OR [Forms]![Search]![txtEnd] Is Null))
  11.   AND  ([Description] Like '*' & [Forms]![Search]![txtDescription] & '*') OR ([Forms]![Search]![txtDescription] Is Null);
The first point to note is that the following two are not equivalent (with reference to your WHERE clause) :
Expand|Select|Wrap|Line Numbers
  1.     ((A=1) OR (A Is Null)
  2. AND (B=1) OR (B Is Null)
  3. AND (C=1) OR (C Is Null))
Expand|Select|Wrap|Line Numbers
  1.     (((A=1) OR (A Is Null))
  2. AND ((B=1) OR (B Is Null))
  3. AND ((C=1) OR (C Is Null)))
In the latter the matching sets of comparisons are linked together by parentheses. This would be important if we wanted to proceed in this way, and would certainly explain why your SQL was giving unexpected results (With reference to your question about ANDs and ORs).

Borrowing from Colintis, we can actually be a little more clever than that, and do a single comparison that handles the situation where the form's control is unset :
Expand|Select|Wrap|Line Numbers
  1. SELECT [ID]
  2.       ,[Project]
  3.       ,[Date]
  4.       ,[Description]
  5.  
  6. FROM   [Historical Photos]
  7.  
  8. WHERE  (([ID] Like Nz([Forms]![Search]![txtPhotoID],'*'))
  9.   AND  ([Project] Like Nz([Forms]![Search]![txtProject],'*'))
  10.   AND  ([Date] Between Nz([Forms]![Search]![txtStart],#1/1/1900#)
  11.                    And Nz([Forms]![Search]![txtEnd],#31/12/9999#))
  12.   AND  ([Description] Like '*' & Nz([Forms]![Search]![txtDescription],'*') & '*'))
Does this make things a little clearer?
Jul 15 '10 #8
katlee
10 New Member
The query seems to be working how it is supposed to! Hopefully I can apply this method to include all the fields.. Thanks to colintis for your patience and Neopa for breaking it down into simpler terms for such a beginner! What a valuable resource this forum is :)
Jul 16 '10 #9
NeoPa
32,557 Recognized Expert Moderator MVP
You're very welcome Katlee :)
Jul 16 '10 #10
katlee
10 New Member
Spoke a little too soon.. I have three combo boxes (cboState, cboAreaOffice, cboRegion) that have queries of my master table as their source..
Expand|Select|Wrap|Line Numbers
  1. AND ([Region] Like Nz ([Forms]![Search]![cboRegion],"*") AND ([AreaOffice] Like Nz ([Forms]![Search]![cboAreaOffice],"*")
... is what I am using now and I get the expected results when selected one state but not the other. Do I need to do something different?
Jul 16 '10 #11
NeoPa
32,557 Recognized Expert Moderator MVP
Katlee, Your SQL doesn't include anything for State, and I have no information as to what States are one or the other.

This leaves me somewhat short of information :D
Jul 16 '10 #12
katlee
10 New Member
Sorry
Expand|Select|Wrap|Line Numbers
  1. AND ([Region] Like Nz ([Forms]![Search]![cboRegion],"*") 
  2. AND ([AreaOffice] Like Nz ([Forms]![Search]![cboAreaOffice],"*") 
  3. AND ([State] Like Nz ([Forms]![Search]![cboState],"*")
To be more specific, I have a dummy set of records right now that I am using to see if the query will work. Currently, records have either "CA" or "AZ" for the state (when the database is in use records will be from all over the US). Using this SQL when choosing "AZ" the query worked fine, but "CA" did not return any records...
Jul 16 '10 #13
NeoPa
32,557 Recognized Expert Moderator MVP
No worries Katlee.

Now you've formulated the SQL readably though, the answer (problem) jumps out.

The closing parenthesis on each line deals only with the Nz() function call. A further closing parenthesis is required to close each sub-statement.

BTW It's not wrong, but possibly unusual, to leave a space after Nz and before the open parenthesis. That's just display though. SQL will handle it either way.

PS. I just checked too, and my suggestion was what led you astray. I didn't include the second closing parenthesis on any of the lines so I will fix that now. It may be fixed by the time you read this, but it certainly was there when you read it. Just so you know (but I don't want to leave misinformation in my post).
Jul 16 '10 #14
katlee
10 New Member
Ok, makes sense!! My SQL is currently
Expand|Select|Wrap|Line Numbers
  1. WHERE (([ID] Like Nz([Forms]![Search]![txtPhotoID],"*")) 
  2. AND ([Project] Like Nz([Forms]![Search]![txtProject],"*")) 
  3. AND ([Date] Between Nz([Forms]![Search]![txtStart],#1/1/1900#) 
  4. And Nz([Forms]![Search]![txtEnd],#31/12/9999#)) 
  5. AND ([Description] Like "*" & Nz([Forms]![Search]![txtDescription],"*") & "*")) 
  6. AND ([Division] Like Nz([Forms]![Search]![txtDivision], "*")) 
  7. AND ([AreaOffice] Like Nz([Forms]![Search]![cboAreaOffice], "*")) 
  8. AND ([Region] Like Nz([Forms]![Search]![cboRegion], "*")) 
  9. AND ([State] Like Nz([Forms]![Search]![cboState], "*"));
... but something strange and perhaps unrelated to the SQL is happening. I have a 8 records with the "CA" in the State field, "Mid-Pacific" in the Region field, and "South-Central California" for the AreaOffice field, but when I select these choices from the combo boxes the query only returns 3 records... any suggestions?

Edit:
I realized that what the three records have in common is they have values in all the other fields being queried (none of the fields were left blank in the table)... the problem is that some records will not have values in a certain field because the information is unknown. For example, very old photographs were assigned Photo IDs and in a certain year this stopped being done. I want users to be able to search by the ID if they know it, but if they want all the records from a certain time period (without knowing and ID) they could enter that and records with or without a Photo ID would be returned (currently only the ones with an ID within that time period are being returned. I hope this makes sense :/
Jul 16 '10 #15
NeoPa
32,557 Recognized Expert Moderator MVP
If it finds some but not others this generally points to something different between the records. As you say nothing about any differences I have nothing to work with I'm afraid.

If you'd like to attach a copy of the database where this behaviour occurs I could look at it for you if you like.

When attaching your work please follow the following steps first :
  1. Remove anything not relevant to the problem. This is not necessary in all circumstances but some databases can be very bulky and some things do not effect the actual problem at all.
  2. Likewise, not entirely necessary in all cases, but consider saving your database in a version not later than 2003 as many of our experts don't use Access 2007. Largely they don't want to, but some also don't have access to it. Personally I will wait until I'm forced to before using it.
  3. If the process depends on any linked tables then make local copies in your database to replace the linked tables.
  4. If you've done anything in steps 1 to 3 then make sure that the problem you're experiencing is still evident in the updated version.
  5. Compile the database (From the Visual Basic Editor select Debug / Compile {Project Name}).
  6. Compact the database (Tools / Database Utilities / Compact and Repair Database...).
  7. Compress the database into a ZIP file.
  8. When posting, scroll down the page and select Manage Attachments (Pressing on that leads you to a page where you can add or remove your attachments. It also lists the maximum file sizes for each of the allowed file types.) and add this new ZIP file.
It's also a good idea to include some instructions that enable us to find the issue you'd like help with. Maybe some instructions of what to select, click on, enter etc that ensures we'll see what you see and have the same problems.
Jul 16 '10 #16
katlee
10 New Member
I realized that what the three records have in common is they have values in all the other fields being queried (none of the fields were left blank in the table)... the problem is that some records will not have values in a certain field because the information is unknown. For example, very old photographs were assigned Photo IDs and in a certain year this stopped being done. I want users to be able to search by the ID if they know it, but if they want all the records from a certain time period (without knowing and ID) they could enter that and records with or without a Photo ID would be returned (currently only the ones with an ID within that time period are being returned. I hope this makes sense..
Jul 16 '10 #17
NeoPa
32,557 Recognized Expert Moderator MVP
Ah. In that case try :
Expand|Select|Wrap|Line Numbers
  1. WHERE ((Nz([ID],'') Like Nz([Forms]![Search]![txtPhotoID],'*'))
  2.   AND (Nz([Project],'') Like Nz([Forms]![Search]![txtProject],'*'))
  3.   AND (Nz([Date],#1/1/1900#) Between Nz([Forms]![Search]![txtStart],#1/1/1900#)
  4.                                  And Nz([Forms]![Search]![txtEnd],#31/12/9999#))
  5.   AND (Nz([Description],'') Like '*' & Nz([Forms]![Search]![txtDescription],'*') & '*'))
  6.   AND (Nz([Division],'') Like Nz([Forms]![Search]![txtDivision], '*'))
  7.   AND (Nz([AreaOffice],'') Like Nz([Forms]![Search]![cboAreaOffice], '*'))
  8.   AND (Nz([Region],'') Like Nz([Forms]![Search]![cboRegion], '*'))
  9.   AND (Nz([State],'') Like Nz([Forms]![Search]![cboState], '*'));
Jul 16 '10 #18
katlee
10 New Member
Wow works like a charm!! Many, many thanks!!!
Jul 16 '10 #19
NeoPa
32,557 Recognized Expert Moderator MVP
A pleasure.

I'm glad it worked for you :)
Jul 16 '10 #20
NBRJ
1 New Member
I just signed up to say thank you to both Katlee and NeoPa.

Katlee for asking the very same question I had (I've got 13 fields in my search of various types). To NeoPa for posting the solution and examples that cover most field types and explaining how it works. That you both posted the code until it was correct was very helpful.

My monster search is working as a result! THANK you both so much :)
Apr 9 '16 #21
NeoPa
32,557 Recognized Expert Moderator MVP
You're very welcome.

We like to stay conscious that our threads are even more useful to others than they are for those involved when they're filled.

Thank you for taking the trouble to post. We appreciate it :-)
Apr 9 '16 #22

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

Similar topics

5
4250
by: MX1 | last post by:
Simpler way to ask question from my previous post. I wrote a query and it has a paramter field in it. I want to enter a date with the current year. If it I put in 6/30/2003, it works great. If I...
1
1737
by: Jim | last post by:
MS-Access 2002 I have a query (Q2) based on a parameter query (Q1) and need to set the parameters within a VBA module. I am using ADO, and opening the query (Q2) as a fordwardonly recordset. ...
3
1931
by: dnl | last post by:
I have a parameter query that I would like to simplify so that I wouldn't need to use wildcards when entering in partial data. The following line is in the criteria field when designing the query:...
2
6501
by: Julie Wardlow | last post by:
Help! I am calculating a future date using the DateAdd function in a query (the calculation also involves an IIf statement), and have managed to get this formula to produce the required result....
0
1384
by: AP | last post by:
I use the following snippet to open a build a querydef. This works great with a query that has a parameter that points to a form, but if I try to use it in a parameter query, even if the parameter...
5
1697
by: vinfurnier | last post by:
Hi - I've been struggling to produce a working parameter query that will allow the end user to type in any date (mm/dd/yy) and obtain the records of the previous 2 days. In other words, if the...
1
1225
by: scolivas | last post by:
I am trying to set up a query that will allow the user to enter just one or maybe three parameters for a specific field. eg... query to prompt user for proj# user wants to pull proj 1 5...
1
1774
by: Mpho Pole | last post by:
Hi everyone.I'm a newly minted member, and I'm glad to be here. Now that the pleasanteries are out of the way, my problem is as follows: I have an SQL query which receives multiple parameters from...
10
2081
hyperpau
by: hyperpau | last post by:
I've been going nuts about it and I've been searching all over the forums to find answers. Can somebody please help me? I am a somehow advanced Access user with basic VBA knowledge. (No ADO or...
4
2670
by: chicane | last post by:
I am currently writing a query in Access. I need to set up a parameter query for three fields which are called Status (text field), Authorised (date field) and Legal notices (date field). I need...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.