how do you use a filter in programming

A

Afrosheen

I'm using a filter for 9 different command buttons to get a report of the
results. The problem is that after I press one button and get the results and
then press another button I get a whole list of my roster instead of the
filtered records. Here is my program.

Filter = ""
Dim stDocName As String
Dim stwhere As String
stwhere = "([cell]=true) and [status] = 'working'"
stDocName = "rpt_select"
FilterOn = True
Filter = strwhere
Me.Requery
DoCmd.OpenReport "rpt_select", acPreview, , stwhere, , "Cell Report"

I'm trying to clear out the filter so the next button I choose will build a
new filter.

Am I doing something wrong?

Thanks for reading this
 
D

Douglas J. Steele

Your variable is named stwhere, but you're setting the Filter to strwhere.
As well, your references to FilterOn and Filter are incorrect: you need to
indicate for what form the filters apply, using either Me.FilterOn and
Me.Filter (assuming you're trying to filter the form in which this code is
running) or Forms![NameOfForm].FilterOn and Forms![NameOfForm].Filter (if
you're trying to filter some other open form)

The fact that you're not getting errors with your code indicates to me that
you haven't told Access to require that all variables be declared. To do
this, go into the VB Editor and select Tools | Options from the menu. On the
first tab, ensure that the checkbox "Require Variable Declaration" is
selected. This will add a line Option Explicit at the top of all future
modules. You'll need to go back to all existing modules and add that line to
each module.

While it may seem like a lot of work to declare all your variables, it will
actually save you hours of debugging in the future in cases such as these.
 
A

Afrosheen

Thanks for the quick reply Doug.
I didn't even see the extra letter in the "str" string. Thanks.

I'm using this all in VBA code. I'm just a newbe when it comes to this
programming. I haven't even considered or know how to use the modules to
declare variables or even pass them. Do you have something on your web sight?
I've check your sight for a few things.

Thanks again..

Douglas J. Steele said:
Your variable is named stwhere, but you're setting the Filter to strwhere.
As well, your references to FilterOn and Filter are incorrect: you need to
indicate for what form the filters apply, using either Me.FilterOn and
Me.Filter (assuming you're trying to filter the form in which this code is
running) or Forms![NameOfForm].FilterOn and Forms![NameOfForm].Filter (if
you're trying to filter some other open form)

The fact that you're not getting errors with your code indicates to me that
you haven't told Access to require that all variables be declared. To do
this, go into the VB Editor and select Tools | Options from the menu. On the
first tab, ensure that the checkbox "Require Variable Declaration" is
selected. This will add a line Option Explicit at the top of all future
modules. You'll need to go back to all existing modules and add that line to
each module.

While it may seem like a lot of work to declare all your variables, it will
actually save you hours of debugging in the future in cases such as these.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Afrosheen said:
I'm using a filter for 9 different command buttons to get a report of the
results. The problem is that after I press one button and get the results
and
then press another button I get a whole list of my roster instead of the
filtered records. Here is my program.

Filter = ""
Dim stDocName As String
Dim stwhere As String
stwhere = "([cell]=true) and [status] = 'working'"
stDocName = "rpt_select"
FilterOn = True
Filter = strwhere
Me.Requery
DoCmd.OpenReport "rpt_select", acPreview, , stwhere, , "Cell Report"

I'm trying to clear out the filter so the next button I choose will build
a
new filter.

Am I doing something wrong?

Thanks for reading this
 
D

Douglas J. Steele

Other than the fact that every example on my code explicitly declares all of
my variables, no, there's nothing specific on my site.

You've already got two declarations in your code:

Dim stDocName As String
Dim stwhere As String

You just need to ensure you've got an appropriate declaration (As Integer,
As Long, As Binary, etc.) for every variable you're using.


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Afrosheen said:
Thanks for the quick reply Doug.
I didn't even see the extra letter in the "str" string. Thanks.

I'm using this all in VBA code. I'm just a newbe when it comes to this
programming. I haven't even considered or know how to use the modules to
declare variables or even pass them. Do you have something on your web
sight?
I've check your sight for a few things.

Thanks again..

Douglas J. Steele said:
Your variable is named stwhere, but you're setting the Filter to
strwhere.
As well, your references to FilterOn and Filter are incorrect: you need
to
indicate for what form the filters apply, using either Me.FilterOn and
Me.Filter (assuming you're trying to filter the form in which this code
is
running) or Forms![NameOfForm].FilterOn and Forms![NameOfForm].Filter (if
you're trying to filter some other open form)

The fact that you're not getting errors with your code indicates to me
that
you haven't told Access to require that all variables be declared. To do
this, go into the VB Editor and select Tools | Options from the menu. On
the
first tab, ensure that the checkbox "Require Variable Declaration" is
selected. This will add a line Option Explicit at the top of all future
modules. You'll need to go back to all existing modules and add that line
to
each module.

While it may seem like a lot of work to declare all your variables, it
will
actually save you hours of debugging in the future in cases such as
these.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Afrosheen said:
I'm using a filter for 9 different command buttons to get a report of
the
results. The problem is that after I press one button and get the
results
and
then press another button I get a whole list of my roster instead of
the
filtered records. Here is my program.

Filter = ""
Dim stDocName As String
Dim stwhere As String
stwhere = "([cell]=true) and [status] = 'working'"
stDocName = "rpt_select"
FilterOn = True
Filter = strwhere
Me.Requery
DoCmd.OpenReport "rpt_select", acPreview, , stwhere, , "Cell Report"

I'm trying to clear out the filter so the next button I choose will
build
a
new filter.

Am I doing something wrong?

Thanks for reading this
 
P

pietlinden

Thanks for the quick reply Doug.
I didn't even see the extra letter in the "str" string. Thanks.

I'm using this all in VBA code. I'm just a newbe when it comes to this
programming. I haven't even considered or know how to use the modules to
declare variables or even pass them. Do you have something on your web sight?
I've check your sight for a few things.

Thanks again..

Douglas J. Steele said:
Your variable is named stwhere, but you're setting the Filter to strwhere.
As well, your references to FilterOn and Filter are incorrect: you needto
indicate for what form the filters apply, using either Me.FilterOn and
Me.Filter (assuming you're trying to filter the form in which this codeis
running) or Forms![NameOfForm].FilterOn and Forms![NameOfForm].Filter (if
you're trying to filter some other open form)
The fact that you're not getting errors with your code indicates to me that
you haven't told Access to require that all variables be declared. To do
this, go into the VB Editor and select Tools | Options from the menu. On the
first tab, ensure that the checkbox "Require Variable Declaration" is
selected. This will add a line Option Explicit at the top of all future
modules. You'll need to go back to all existing modules and add that line to
each module.
While it may seem like a lot of work to declare all your variables, it will
actually save you hours of debugging in the future in cases such as these.
Afrosheen said:
I'm using a filter for 9 different command buttons to get a report ofthe
results. The problem is that after I press one button and get the results
and
then press another button I get a whole list of my roster instead of the
filtered records. Here is my program.
   Filter = ""
   Dim stDocName As String
   Dim stwhere As String
   stwhere = "([cell]=true) and [status] = 'working'"
   stDocName = "rpt_select"
   FilterOn = True
   Filter = strwhere
   Me.Requery
   DoCmd.OpenReport "rpt_select", acPreview, , stwhere, , "Cell Report"
I'm trying to clear out the filter so the next button I choose will build
a
new filter.
Am I doing something wrong?
Thanks for reading this

To add to what Doug said, you should probably add the following line
to each of your sub routines...
Option Explicit

it forces all variables to be declared before you use them. That way,
if you make a typo, the code won't compile and you can find the error
more easily. (Adding error handling helps a lot too, just so you know
where the error is coming from).
 

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