Solved

ms access filter query with empty combobox

Posted on 2014-01-23
2
1,838 Views
Last Modified: 2014-01-23
Hi,

I have a query that i run from a command button on a form that exports the results to an excel spreadsheet. On the form I have a combobox (cboFacilityRefID) to filter the results of the query. This works fine when a value is selected in the combobox. However, if no value is selected the query returns no results. What i want to achieve is for all records to be returned when the combobox is empty.

I have tried the following but this doesnt seem to work

IIf(IsNull([Forms]![frmNavMenuForm].[NavMenuSubform].[Form]![Reports_Reports]![cboFacilityRefID]),True,[Forms]![frmNavMenuForm].[NavMenuSubform].[Form]![Reports_Reports]![cboFacilityRefID])

PS the FacilityRefID field in the underlying table in the query will always have a value in it (either 1,2, or 3 at the moment but will increase to 4,5 or 6 in time).

Many thanks
0
Comment
Question by:andrewpiconnect
2 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 39804579
Try something like:

WHERE [yourField] = [Forms]![frmNavMenuForm].[NavMenuSubform].[Form]![Reports_Reports]![cboFacilityRefID]
OR [Forms]![frmNavMenuForm].[NavMenuSubform].[Form]![Reports_Reports]![cboFacilityRefID] & "" = ""

This basically says: limit my results to those records where [yourField] matches the value in the combo box on the form, or all records if the combo box is empty.

You would get the 2nd part of this WHERE clause by putting the reference to the control in the "Field" row of the query design grid, as though it were a computed column (which is what it actually becomes).
0
 

Author Closing Comment

by:andrewpiconnect
ID: 39804638
excellent. it worked a treat!

On the same basis then I am trying to filter the results further using a date range as follows on the [InceptDate] field in the query:

Between [Forms]![frmNavMenuForm].[NavMenuSubform].[Form]![Reports_Reports]![txtStartDate] And [Forms]![frmNavMenuForm].[NavMenuSubform].[Form]![Reports_Reports]![txtEndDate]

The idea is to filter the results between a date range and combobox but also allow the query the return all values when no date range or combobox value is selected (the [InceptDate] field will always have a date it in as its a compulsory field elsewhere in the database.

can i incoorporate both filters in the same query?
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql server concatenate fields 10 33
Query Syntax 17 34
Opening forms with an input box 5 20
2 IIF's in Access query 25 23
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question