Filter form on load (show this years records only)

  • Thread starter Russ via AccessMonster.com
  • Start date
R

Russ via AccessMonster.com

Help....

I need to open a form setting the filter as to show all records for the
current year, I have a field called DATE, this is the field that indicates
the record date. How can I set a form filter so when the form opens it only
shows current year records. then the user can click the remove filter and see
all records.

Thanks in advance
 
A

Allen Browne

Use the form's Open event procedure to apply a filter.

This kind of thing:

Private Sub Form_Open(Cancel As Integer)
Me.Filter = "[DATE] >= #" & Year(Date) & "/1/1#"
Me.FilterOn = True
End Sub

Note that DATE is a reserved word, so not a good name for a field. In some
contexts, Access confuses it with the system date, and so does not yield the
results you expect. Consider renaming the field to InvoiceDate or something.
 
R

Russ via AccessMonster.com

Allen, thanks for the help, but not having luck with it, it gives an error
has no value.

Private Sub Form_Open(Cancel As Integer)

Me.Filter = "[MaintDATE]>= #" & Year(MaintDate) & "/1/1#"
Me.FilterOn = True

End Sub

Allen said:
Use the form's Open event procedure to apply a filter.

This kind of thing:

Private Sub Form_Open(Cancel As Integer)
Me.Filter = "[DATE] >= #" & Year(Date) & "/1/1#"
Me.FilterOn = True
End Sub

Note that DATE is a reserved word, so not a good name for a field. In some
contexts, Access confuses it with the system date, and so does not yield the
results you expect. Consider renaming the field to InvoiceDate or something.
[quoted text clipped - 5 lines]
see
all records.
 
D

Douglas J. Steele

While the field in your table shouldn't be named Date (and I see you changed
it), you need to call the Date function when setting the function:

Me.Filter = "[MaintDATE]>= #" & Year(Date) & "/1/1#"


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Russ via AccessMonster.com said:
Allen, thanks for the help, but not having luck with it, it gives an error
has no value.

Private Sub Form_Open(Cancel As Integer)

Me.Filter = "[MaintDATE]>= #" & Year(MaintDate) & "/1/1#"
Me.FilterOn = True

End Sub

Allen said:
Use the form's Open event procedure to apply a filter.

This kind of thing:

Private Sub Form_Open(Cancel As Integer)
Me.Filter = "[DATE] >= #" & Year(Date) & "/1/1#"
Me.FilterOn = True
End Sub

Note that DATE is a reserved word, so not a good name for a field. In some
contexts, Access confuses it with the system date, and so does not yield
the
results you expect. Consider renaming the field to InvoiceDate or
something.
[quoted text clipped - 5 lines]
see
all records.
 
R

Russ via AccessMonster.com

Thanks Douglas..

Ok, changed it back to date but, I get run time error saying can not find
field date.
Whatelse am I missing?

Me.Filter = "[MaintDATE]>= #" & Year(Date) & "/1/1#"
While the field in your table shouldn't be named Date (and I see you changed
it), you need to call the Date function when setting the function:

Me.Filter = "[MaintDATE]>= #" & Year(Date) & "/1/1#"
Allen, thanks for the help, but not having luck with it, it gives an error
has no value.
[quoted text clipped - 26 lines]
 
D

Douglas J. Steele

Does the error say it can't find Date or that it can't find MaintDATE?

If it's complaining about MaintDATE, are you sure that that's what you named
the field in your table?

If it's complaining about Date, check whether the Date function works for
you. Go to the Immediate Window (Ctrl-G), type

?Date()

(complete with the question mark), then hit Enter. Do you get a date, or an
error?

If you get an error, there are two common reasons.

The first is that you've used Date somewhere else in your database: a field
in a table, a control on a form, or something like that. Date is a reserved
word, and should never be used in anything you created in the database. Find
that usage, and correct it.

The other possibility is that your references are messed up.

References problems can be caused by differences in either the location or
file version of certain files between the machine where the application was
developed, and where it's being run (or the file missing completely from the
target machine). Such differences are common when new software is installed.

On the machine(s) where it's not working, go into the VB Editor and select
Tools | References from the menu bar. Examine all of the selected
references.

If any of the selected references have "MISSING:" in front of them, unselect
them, and back out of the dialog. If you really need the reference(s) you
just unselected (you can tell by doing a Compile), go back in and reselect
them.

If none have "MISSING:", select an additional reference at random, back out
of the dialog, then go back in and unselect the reference you just added. If
that doesn't solve the problem, try to unselect as many of the selected
references as you can (Access may not let you unselect them all), back out
of the dialog, then go back in and reselect the references you just
unselected. (NOTE: write down what the references are before you delete
them, because they'll be in a different order when you go back in)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Russ via AccessMonster.com said:
Thanks Douglas..

Ok, changed it back to date but, I get run time error saying can not find
field date.
Whatelse am I missing?

Me.Filter = "[MaintDATE]>= #" & Year(Date) & "/1/1#"
While the field in your table shouldn't be named Date (and I see you
changed
it), you need to call the Date function when setting the function:

Me.Filter = "[MaintDATE]>= #" & Year(Date) & "/1/1#"
Allen, thanks for the help, but not having luck with it, it gives an
error
has no value.
[quoted text clipped - 26 lines]
see
all records.
 
R

Russ via AccessMonster.com

Douglas,

Thanks for taking the time out to help,
It was an error on my part, when I looked closer at the query the form was
based on the field was not set to the specicif date format, once I changed
that all is good. Again thanks for your time and help.
Does the error say it can't find Date or that it can't find MaintDATE?

If it's complaining about MaintDATE, are you sure that that's what you named
the field in your table?

If it's complaining about Date, check whether the Date function works for
you. Go to the Immediate Window (Ctrl-G), type

?Date()

(complete with the question mark), then hit Enter. Do you get a date, or an
error?

If you get an error, there are two common reasons.

The first is that you've used Date somewhere else in your database: a field
in a table, a control on a form, or something like that. Date is a reserved
word, and should never be used in anything you created in the database. Find
that usage, and correct it.

The other possibility is that your references are messed up.

References problems can be caused by differences in either the location or
file version of certain files between the machine where the application was
developed, and where it's being run (or the file missing completely from the
target machine). Such differences are common when new software is installed.

On the machine(s) where it's not working, go into the VB Editor and select
Tools | References from the menu bar. Examine all of the selected
references.

If any of the selected references have "MISSING:" in front of them, unselect
them, and back out of the dialog. If you really need the reference(s) you
just unselected (you can tell by doing a Compile), go back in and reselect
them.

If none have "MISSING:", select an additional reference at random, back out
of the dialog, then go back in and unselect the reference you just added. If
that doesn't solve the problem, try to unselect as many of the selected
references as you can (Access may not let you unselect them all), back out
of the dialog, then go back in and reselect the references you just
unselected. (NOTE: write down what the references are before you delete
them, because they'll be in a different order when you go back in)
Thanks Douglas..
[quoted text clipped - 16 lines]
 
Top