Newbie: Filter w commandbutton

Jeblanek

New Member
Joined
Dec 31, 2004
Messages
1
Hi!

I'd like to filter my form, based on a query(called filterhez). I added a command button and I tried to write the code, but the ApplyFilter method doesn't seem to work. :(

Here is the code:

Dim filt As AllQueries
Set filt = Access.AllQueries("filterhez")

DoCmd.ApplyFilter filt

Please be detailed :rolleyes:
THX
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
If you go to the built-in help, inside Access, you get this example under ApplyFilter

Code:
DoCmd.ApplyFilter , "LastName = 'King'"

A filter just looks like a SQL parameter.
Aka, the part that goes:

"WHERE fieldname1 = 'alpha'"

But you drop the 'WHERE' out of it.

edit - I haven't tested this or used this method (ever actually) - I'm not sure if ApplyFilter forces a screen refresh (it may apply the filter but not change the information in your form.

If it doesn't, do this (in addition) DoCmd.Requery

This forces the form to requery the recordsource - and since you applied the filter, it will do so considering the filter. The above method is not an invalid method, it's just not the one I tend to use. Like many things in life, there's more than one way to approach any given problem - some might be better than others, and then again, all might be equal.

My normal preference is to write a full SQL line as a form recordsource.
aka "SELECT * FROM tblName WHERE fld1 = 'a_value'"

In a single line, I've managed everything, including the filter.

Mike
 
Upvote 0

Forum statistics

Threads
1,221,893
Messages
6,162,659
Members
451,780
Latest member
Blake86

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top