Multiple Filter Critieria with Single Button Click

A

Angelsnecropolis

I have one textbox (Date) and one combobox (ID) that I need to be filtered
with a single button click with the info that is currently typed into them.
I've been able to find info about filtering on click for 1 using:

ID.SetFocus
RunCommand acCmdFilterBySelection

or

Me.Filter = "[ID] = """ & Me.ID & """"
Me.filteron = True

But they don't work when trying to get info from 2 fields. The filter is
applied to records that span a full year. If the filter is set on the Date
the ID field changes, which I don't want and vise versa for ID then Date.

So basically I need to figure out how to filter for ID AND Date at the same
time instead of filter one and then filter the other.

Any help? Thanks!
 
A

Albert D. Kallal

The following is a post of mine using the "where" clause for a reprot, but
the advice should work well for a filter..

===========

Use the "where" clause, and make the reports sql *without* any
parameters..and you not need to change the params..

So, you can build a un-bound form (a un-bound form is a form that is NOT
attached to a table - these forms are typically designed for user interface
face stuff like prompts, print buttons etc).

The following screen shots are all un-bound forms, and they simply prompt
the user for information.

http://www.members.shaw.ca/AlbertKallal/ridesrpt/ridesrpt.html

The above should give you some ideas

So, the solution use now is simply to take the values from the form, and
build your own where clause in code. That way, you simply design the reports
(or forms), and attached them to the query. And, NO FORMS conditions are
placed in the query.

To "send" the conditions to the report (or form), you simply use the "where"
clause. This is exactly why ms-access has this feature...and it solves a
zillion problems...and will reduce your development costs by a substantial
amount.

The code to make those above screens work and launch the report with the
selected restrictions when you hit the "print" button is easy:


dim strWhere as string

select sales rep combo

if isnull(cboSalesRep) = false then

strWhere = "SalesRep = " & cboSalesRep & ""

end if

select what City for the report

if isnull(cboCity) = false then
if strWhere <> "" then
strWhere = strWhere " and "
endif
strWhere = strWhere & "City = " & cobCity & ""
end if

Note how the 2nd combo test is setup. You can add as "many" more conditions
you want. Lets say we have a check box to only include Special Customers. We
can add to our very nice prompt screen a check box to

[x] Show Only Special customers

The code we add would be:

if chkSpeicalOnly = True then
if strWhere <> "" then
strWhere = strWhere " and "
endif
strWhere = strWhere & "SpecialCust = true"
endif

For sure, each combo and control we add to the nice report screen takes a
bit of code, but no more messy then the query builder..and this way, each
query is nice and clean, and free of a bunch of HIGHLY un-maintainable
forms! expressions.

Further, it means you can re-use the same query for different reports, and
have no worries about some form that is supposed to be open. So, a tiny bit
more code eliminates the messy query problem.. For me, this is very worth
while trade.

For a date range, we could put two calendar controls on the screen. The code
could be:


dim strWhere as string
dim strStartDate as string
dim strEndDate as string


strStartDtae = "#" & format(me.StartDateContorl,"mm/dd/yyyy") & "#"
strEndDate = "#" & format(me.EndDateContorl,"mm/dd/yyyy") & "#"

strWhere = "InvoiceDate is between " & strStartDate & " and " & strEndDate

docmd.openReport "InvoiceReport",acViewPreview,,strWhere
 
A

Allen Browne

The basic idea is like this:

Dim strWhere As String
strWhere = "(ID = """ & Me.txtID & """) AND ([SomeDate] = " & _
Format(Me.txtDate, "\#mm\/dd\/yyyy\#") & ")"
Me.Filter = strWhere
Me.FilterOn = True

Things to note:
a) You need to use *unbound* controls to enter the ID and date.
Otherwise you are changing the values in the current record.

b) If your ID field is a Number, drop the extra quotes.

c) Do not change the formatting to match your regional dates.
The string needs the US date format.

d) It will fail if the text box doesn't have a valid date (e.g. if left
blank.)

To help debug it, add the line:
Debug.Print strWhere
Then open the Immediate Window (Ctrl+G), and see what came out. Compare that
to the WHERE clause in a query that does work.

For more detail, download the example database in this article:
Search form - Handle many optional criteria
at:
http://allenbrowne.com/ser-62.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

message
news:[email protected]...
 
A

Angelsnecropolis

Thanks. I did figured I would need an unbound control. I used a textbox. I
suppose I went the long way around. On button click on set it to take the ID
and make textbox = IDbox then filter by Date. Then make IDbox = texbot and
then filter by IDbox. Thanks for your replies.

Allen Browne said:
The basic idea is like this:

Dim strWhere As String
strWhere = "(ID = """ & Me.txtID & """) AND ([SomeDate] = " & _
Format(Me.txtDate, "\#mm\/dd\/yyyy\#") & ")"
Me.Filter = strWhere
Me.FilterOn = True

Things to note:
a) You need to use *unbound* controls to enter the ID and date.
Otherwise you are changing the values in the current record.

b) If your ID field is a Number, drop the extra quotes.

c) Do not change the formatting to match your regional dates.
The string needs the US date format.

d) It will fail if the text box doesn't have a valid date (e.g. if left
blank.)

To help debug it, add the line:
Debug.Print strWhere
Then open the Immediate Window (Ctrl+G), and see what came out. Compare that
to the WHERE clause in a query that does work.

For more detail, download the example database in this article:
Search form - Handle many optional criteria
at:
http://allenbrowne.com/ser-62.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

message
I have one textbox (Date) and one combobox (ID) that I need to be filtered
with a single button click with the info that is currently typed into
them.
I've been able to find info about filtering on click for 1 using:

ID.SetFocus
RunCommand acCmdFilterBySelection

or

Me.Filter = "[ID] = """ & Me.ID & """"
Me.filteron = True

But they don't work when trying to get info from 2 fields. The filter is
applied to records that span a full year. If the filter is set on the Date
the ID field changes, which I don't want and vise versa for ID then Date.

So basically I need to figure out how to filter for ID AND Date at the
same
time instead of filter one and then filter the other.

Any help? Thanks!
 

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