Solved

Creating Toggle Filter Button in Access VBA

Posted on 2012-03-26
16
2,092 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

738 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