Solved

ms access filter query with empty combobox

Posted on 2014-01-23
2
1,777 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now