Filter Query Criteria based on check box

OasisUnknown

New Member
Joined
Oct 15, 2015
Messages
46
Hello All,

I am trying to filter a query based off a check box that is on my form.


the field I am trying to filter either has text values in them or they are null

What I am trying to accomplish is if the check box is not checked then just show me all the records. (basically don't filter anything and just show all. nulls and texts included.

when the check box is checked only show me null values and nothing with text in them.


in my field criteria I am trying something like

iif([Forms]![MyForm]![MyCheckbox] = 0, "*", Is Null)

but this is not working in any reguard but if I do a search for some text that is in there such as.

iif([Forms]![MyForm]![MyCheckbox] = 0, "myText", Is Null)

I will get an error because of the is null. it says the expression is to difficult to calculate.

but if I do

iif([Forms]![MyForm]![MyCheckbox] = 0, "myText", "myOtherText")

my query will flip back and forth between the fields with myText and myOtherText as expected.

I am not really sure what the best way to go about this is.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
With iif([Forms]![MyForm]![MyCheckbox] = 0, "*", Is Null) you are trying to use an operator, not Null.
You could try iif([Forms]![MyForm]![MyCheckbox] = 0, "*", Null), but I'm not sure if that will work. Let us know and maybe we can suggest something else, such as applying a filter or where clause in vba.
Usually, I'd do this by having all the form records loaded, then filtering on checkbox selection and applying a filter (and removing it if the box is deselected).

 
Upvote 0
Query criteria will not accept IIF() method. You can use a small VBA Routine that runs on the After_Update() event of the check-box. Sample Code is given below for reference:
Code:
Private Sub Check38_AfterUpdate()
Dim x As Variant, q As QueryDef
Dim strSQL As String


Set q = CurrentDb.QueryDefs("Query57")
strSQL = "SELECT Employees.[Last Name], Employees.[ZIP/Postal Code] FROM Employees "
x = Me!Check38
Select Case x
       Case True
       strSQL = strSQL & " WHERE ([Last Name] Like '???*');"
       q.SQL = strSQL
      Case False
      q.SQL = strSQL & " WHERE ([Last Name] is Null OR [Last Name]='');"
End Select
CurrentDb.QueryDefs.Refresh
DoCmd.OpenReport "Query57", acViewPreview
      
End Sub

When check-box is unchecked Records with Last-Name empty field will appear on the report. When checked all records except empty Last-Name field records will be displayed on the Report.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,805
Messages
6,162,074
Members
451,738
Latest member
gaseremad

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top