473,623 Members | 3,365 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 8305
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,566 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,566 Recognized Expert Moderator MVP
You're very welcome Katlee :)
Jul 16 '10 #10

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

Similar topics

5
4278
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 put in some kind of a variable based input for the parameter like '6/30/' & year(now()), it doesn't work. Is there any way to concatenate text and a variable at the input of a parameter query. THIS IS DRIVING ME NUTS. PLEASE HELP. :)
1
1742
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. Since Q1 isn't explictly opened in the VBA code, I'm not sure how to set the parameters. I do not want to run Q1 to load a table and then have Q2 based on the table
3
1950
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: Like Running the query brings up the dialog box to enter the parameter. If the name I am looking for is Smith, I can type "smi*" to do a wildcard match. Is there anyway to avoid needing the wildcard? I'd like my less- knowledgeable database...
2
6510
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. I then want to search through the records and select those with dates (as caluclated above) within a user defined range, and so I am using a parameter query. However, this query returns dates outside of the range and appears to have particular...
0
1387
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 is defined I get the following error 2482 Access cant find the name 'enter year' you entered in your expression. 'This is the code snippet I use to open see below for an example of
5
1703
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 user types in 08/07/06, the records of 08/05/06, 08/06/06 and 08/07/06 will all appear in the qry. I have the following code, which is real basic, but I am running into "the expression is typed incorrectly or is too complex etc".
1
1234
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 and 9 all at once the first time, next time they only want to pull proj 12. when I tried putting 1 or 5 or 9
1
1779
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 a form depending on the users' choice. Most of the parameters are in drop down combos on the said form and I would like my SQL statement to return results according to the values of the drop downs regardless of the number of drop downs. The drop...
10
2092
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 SQL) I have created this database for a Repair Center company to keep track of items that are received, transferred to a third party technicians, and released back to customers. I created 3 Main tables with the following fields. 1) ...
4
2677
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 to set it up so that the user can enter info into each of the fields and run the query from that but I also need to set it up so that if nothing is entered into the parameter boxes that it needs to return all results. I have set up the first...
0
8162
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8662
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8317
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6104
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1468
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.