Filtering a form by several fields

sagi871

New member
Local time
Today, 08:15
Joined
Aug 6, 2014
Messages
3
Hey,

I'm using 4 combo-boxes in order to filter a tabular form in access.
One combo box is for a company name the second is for a report type and the third and forth is for choosing years.
I used VBA in order to apply the filter:

Code:
Me.Filter = "[Company].Value Like ""*"" & '" & Combo108.Value & "' & ""*"""
Me.Filter = "[Report_Type] =" & "'" & Me.Combo123.Value & "'" & ""
Me.Filter = "[Rep_Year] Between " & Combo125.Value & " and " & Combo127.Value

Each line works by itself but I cant figure out how to combine them together, or how to use "AND" between each filter so the filtering will refer all of them when I run the code.
 
You have to use AND to join them togeather

Code:
Me.Filter = "([Company].Value Like '*" & Me.Combo108 & "*') AND ([Report_Type] = '" & Me.Combo123 & "') AND ([Rep_Year] Between " & Me.Combo125 & " AND " & Me.Combo127 & ")"
Me.FilterOn = True
 
Thanks it works!
but if one of the comboxes is empty then I get syntax error. Is there a way the solve it?
 
You can always use Nz() function. Something like.
Code:
Me.Filter = "([Company].Value Like '*" & [COLOR=Red][B]Nz([/B][/COLOR]Me.Combo108[COLOR=Red][B], "")[/B][/COLOR] & "*') AND " & _
            "([Report_Type] = '" & [COLOR=Red][B]Nz([/B][/COLOR]Me.Combo123[COLOR=Red][B], "")[/B][/COLOR] & "') AND " & _
            "([Rep_Year] Between " & [COLOR=Red][B]Nz([/B][/COLOR]Me.Combo125[COLOR=Red][B], -365)[/B][/COLOR] & " AND " & [COLOR=Red][B]Nz([/B][/COLOR]Me.Combo127[COLOR=Red][B], 9999)[/B][/COLOR] & ")"
Me.FilterOn = True
 
Thank you,
Didn't know this one exists. It works perfectly.
 
You are welcome ! Good luck.

picture.php
 
#6: That blended in nicely with your post Paul. Almost got me confused. ;)
 

Users who are viewing this thread

Back
Top Bottom