Avatar of pdvsa
pdvsa
Flag for United States of America asked on

How to filter a combo box on a form?

Hello, I have a form and I want to filter a combo box on this form.  I am having a difficult time doing this.  I click the small drop down icon on the header and it gives me an option to filter by what text I type but it doesnt not filter. I have created a button on the form and chose Apply Filter but that doesnt work either.  A macro is created in the button but there is no code in the macro.

How can I create a filter on a form to filter a combo box on the form?  I want to be able to choose what text to filter on.  

thank you.
Microsoft Access

Avatar of undefined
Last Comment
pdvsa

8/22/2022 - Mon
John Tsioumpris

You need to filter the combobox's ControlSource not the form's
e.g your combobox has a controlSource :
SELECT ImportantFIeld From YourTable

Open in new window

if you want to filter it
SELECT ImportantFIeld From YourTable WHERE SomeOtherField = " & Me.txbBoxCriteria  ' If the txbBoxCriteria  carries Numeric Values

Open in new window

or
SELECT ImportantFIeld From YourTable WHERE SomeOtherField = '" & Me.txbBoxCriteria & "'"  ' If the txbBoxCriteria  carries AlphaNumeric Values

Open in new window

Gustav Brock

Most likely, your lookup field has a combox bound to the ID field, not the field holding the values displayed.

When you click and open the filterbox, you can filter on the ID only, not the displayed value.


So, this is not possible. you must choose another concept.

Mark Edwards

One design trick that I use a lot is to use the combobox's GotFocus event to run the code that applies any changes to the droplist (rowsource).

Set your filter text in your textbox, and on the combobox GotFocus event, put the code to set the rowsource SQL to what you want with the filter added as John shows above.

This way, you never have to worry about the combobox having the latest rowsource list as it is updated every time you click on the combobox!

Not sure how your combobox is designed, but you can build an SQL statement in code and apply it directly to the .rowsource property of the control.
If you are using a querydef object, you can wrap the querydef as the data source (instead of a table) in an SQL statement with the WHERE filter clause.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
pdvsa

ASKER

I am pretty lost.  

Maybe its best to look at my db. 

I pared it down. 

I want to be able to filter cbo for "sent to HB"


thank you....


Gustav Brock

Tony, that file contains a table and a report - no form, no combobox ...

pdvsa

ASKER

HI Gustav, I accidentally attached the wrong db.  So sorry about that.  I posted it below.

EE_Filter2.accdb

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Gustav Brock

OK.

As said, you can't do that. If you unhide the first column of the combobox, you can set the filter to, say, 2, and it will filter.


However, this method can only filter on the bound column, and this column must be visible.


PS: Hope you are doing well. Missing SA and the sand? I guess not.

pdvsa

ASKER

Hi Gustav, 


thanks for the response.  I was looking to implement the Got Focus event with John's code.  I am not sure if you are referring to this or not. 


Yes, I am glad I am back in the USA.  Saudi was not fun.  

pdvsa

ASKER

Mark


I am trying to follow John's code. 

I am putting the below in teh row source of the  cboStatus2

I am sure I am not following becuase I dont think I can refer to Me in the where condition like I am. 


SELECT tblStatus.ID, tblStatus.Status 

where cboFilterFavorites = " & Me.cboStatus2

FROM tblStatus ORDER BY tblStatus.Status



Gustav:

< If you unhide the first column of the combobox, you can set the filter to, say, 2, and it will filter.

Yes, I tried and I can sort but its not as useful to filter on the number instead of the text. 


Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
pdvsa

ASKER

Hi John, I am implementing your code. If you have a chance to respond...

Gustav Brock

I was looking to implement the Got Focus event with John's code.  I am not sure if you are referring to this or not.

 

I was not. I referred to your sample database.

Please note, that the various methods using traditional code will not work as they are not related to what you try to: to set the filter for a field.


So, as already mentioned, you will have to rethink your concept.



John Tsioumpris

I am here...:) what exactly is the issue?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
John Tsioumpris

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
pdvsa

ASKER

wow that is nice.  It works in the uploaded db above.  I am trying to copy the code over and when I click on the dropdown and select "Sent to HB" in my db I get a msg box prompt for UStatus?  Could you kindly let me know what I am doing wrong?  

the below is what I copied into my db and no other codes or cbo.  

Private Sub cboFilterFavorites_AfterUpdate()

Me.Filter = "UStatus = '" & Me.cboFilterFavorites.Column(1) & "'"
Me.FilterOn = True

End Sub

John Tsioumpris

If you see i have added a new field in the RecordSource of the form..
The initial problem was that there was a mismap between the status on the combo and the status on the bottom form...so i had to work on text level.
pdvsa

ASKER

Hi John, sorry for my late reply.  I now see the extra field.  I see that you also changed tblProjects.status from text to number property.  I somehow opened up the wrong db.  Nice craftiness indeed.   Thanks again.  

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes