Remove part of a filter

B

BruceM

I'm trying to find the best way of applying or removing part of a filter.
In a Purchase Order database I have a command button with the following code
in the Click event to filter for just the Purchase Orders by the vendor on
the current record:

Dim strThisVendor As String
strThisVendor = "VendorID =" & Me.VendorID

Me.FilterOn = Not Me.FilterOn
If Me.FilterOn = True Then
DoCmd.ApplyFilter , strThisVendor
End If

This works fine for setting or removing the filter. Now I want the option
of filtering according to whether the PO is approved. The presence of a
name in the Approved field is the criteria. That command button code may be
something like:

Dim strApproved As String
strApproved = "Approved =""" & Me.Approved & """"

Me.FilterOn = Not Me.FilterOn
If Me.FilterOn = True Then
DoCmd.ApplyFilter , strApproved
End If

However, I want these two filters to work independently of each other. That
is, I would like the ability to filter for the current vendor, then to apply
or remove the second filter without affecting the first filter. As it
stands, if I have filtered for one vendor's POs, FilterOn = True, so the
second filter turns off the first filter, which means the second filter
won't be applied.

I wonder if I should have something like:
Public strThisVendor as String
and so forth for all of the filter strings in the Declarations section of
the form's code module, and to combine the strings in a Public sub that I
would call in the Click event of each command button. I suppose each string
could begin with AND:
strThisVendor = "AND VendorID =" & Me.VendorID
At the end of establishing the filter I could lop off the first four letters
in order to get a meaningful string. However, I really don't know if this
will work at all, or if it will work whether it is a reasonable approach, or
whether I am overlooking something simpler.
 
B

BruceM

Thanks for the reply, and for taking time to write out the code. This makes
perfect sense, now that I can see it in front of me. I wasn't thinking of
toggle buttons at all. I see that I could add several other filters in the
same general way, all within the one function. Do I understand correctly
that "If Me.tglVendor Then" is the same as "If Me.tglVendor = True Then"?
In other words, is "True" the default, or is something else going on there?
Let's say the form is named frmPO. Does this function appear in frmPO's
code module? If it does, does the fact that it is Private mean that it can
only be called from frmPO?
I like the idea of changing the fore color to show which filters are
applied. That's another angle that didn't occur to me.
 
B

BruceM

Thanks again. Not only does this give me a way to do the filtering, but
also reminds me about toggle buttons, which I tend to overlook. Thanks too
for the explanation about the Boolean values.
I'll have to take a look at that loop business too. I have seen that type
of code here and there, but have never quite understood the point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top