Solved

Creating Toggle Filter Button in Access VBA

Posted on 2012-03-26
16
1,994 Views
Last Modified: 2012-03-26
I would like to code a button that will essentially do what the "toggle filter" button does in the Access ribbon.  I would like it to filter on the current field's value.  I would then like a button to remove it.  Does anyone know how to do this?
0
Comment
Question by:marku24
  • 6
  • 5
  • 4
  • +1
16 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 37766815
Your Filter On code (for Numeric data):

Me.Filter = "[YourField] = " & txtYourIDField
Me.FilterOn = TRUE

Or for TEXT data:

Me.Filter = "[YourField] = " & chr(34) &  txtYourField & chr(34)
Me.FilterOn = TRUE




To toggle the filter off:

Me.Filter = ""
Me.FilterOn = False
0
 

Author Comment

by:marku24
ID: 37766841
thank you - it could be one of three text fields.  How do I tell the script which is my current / active field?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37766861
Give this a try:


Me.Filter = Me.ActiveControl.Controlsource =  & Chr(34)  & Me.ActiveControl & chr(34)
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 61

Expert Comment

by:mbizup
ID: 37766865
Or:

Me.Filter = Me.ActiveControl.Controlsource & " = "  & Chr(34)  & Me.ActiveControl & chr(34)
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 37766972
Actually, once you have the form filtered, your button that toggles the filter on and off can simply change the FilterOn property of the form.

Private Sub cmd_FilterToggle_Click

    me.FilterOn = Not me.FilterOn

End Sub
0
 

Author Comment

by:marku24
ID: 37767036
The filter doesn't work - Run time error 438:  Object doesn't support this property or method.  any ideas?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37767065
Sorry - if you're clicking a command button after entering the data needed, youll have to refer to the PREVIOUS control (the active control is the command button):

Me.Filter = Screen.PreviousControl.Controlsource =  & Chr(34)  & Screen.PreviousControl & chr(34)
0
 
LVL 61

Accepted Solution

by:
mbizup earned 200 total points
ID: 37767086
Actually, try this:


Me.Filter = "[" & Screen.PreviousControl.Controlsource & "] = "  & Chr(34)  & Screen.PreviousControl & chr(34)
0
 

Author Comment

by:marku24
ID: 37767147
The filter off does work but the filter code above does not.  It doesn't error but does not filter.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 37767179
Mark,

You have 15 responses.  Without indicating which idea you are responding to it is a little difficult.  Can you provide the code you are currently using?
0
 

Author Comment

by:marku24
ID: 37767234
sorry - currently using this one:

Me.Filter = "[" & Screen.PreviousControl.Controlsource & "] = "  & Chr(34)  & Screen.PreviousControl & chr(34)
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37767246
Did you include the following after that line?

Me.filteron=true
0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 50 total points
ID: 37767250
Try:

Me.Filter = "[" & Screen.PreviousControl.Controlsource & "] = "  & Chr(34)  & Screen.PreviousControl.Value & chr(34)

But make sure you have the following line following that:

Me.FilterOn = true
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 37767256
Or, better yet, try:

Dim strCriteria as string

strCriteria = "[" & Screen.PreviousControl.Controlsource & "] = "  & Chr(34)  & Screen.PreviousControl.Value & chr(34)

debug.print strCriteria

Me.Filter = strCriteria
me.FilterOn = true
0
 

Author Closing Comment

by:marku24
ID: 37767267
thank you
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37767270
<No Points wanted>

Yes please also state if you are filtering for text or numeric values as well...
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question