Solved

Creating Toggle Filter Button in Access VBA

Posted on 2012-03-26
16
1,908 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
Comment Utility
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
Comment Utility
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
Comment Utility
Give this a try:


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

Expert Comment

by:mbizup
Comment Utility
Or:

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

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Actually, try this:


Me.Filter = "[" & Screen.PreviousControl.Controlsource & "] = "  & Chr(34)  & Screen.PreviousControl & chr(34)
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

Author Comment

by:marku24
Comment Utility
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)
Comment Utility
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
Comment Utility
sorry - currently using this one:

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

Expert Comment

by:mbizup
Comment Utility
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
Comment Utility
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)
Comment Utility
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
Comment Utility
thank you
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
<No Points wanted>

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now