We help IT Professionals succeed at work.

How to filter a combo box on a form?

pdvsa
pdvsa asked
on
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.
Comment
Watch Question

John TsioumprisSoftware & Systems Engineer
Distinguished Expert 2019

Commented:
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

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:

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 EdwardsChief Technology Officer

Commented:
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.
pdvsaProject finance

Author

Commented:

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....


Most Valuable Expert 2015
Distinguished Expert 2018

Commented:

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

pdvsaProject finance

Author

Commented:

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

EE_Filter2.accdb

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:

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.

pdvsaProject finance

Author

Commented:

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.  

pdvsaProject finance

Author

Commented:

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. 


pdvsaProject finance

Author

Commented:

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

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:

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 TsioumprisSoftware & Systems Engineer
Distinguished Expert 2019

Commented:
I am here...:) what exactly is the issue?
Software & Systems Engineer
Distinguished Expert 2019
Commented:
Well ... i must admit...i got a little confused over your implementation.
I hope you don't mind but i made some small changes and now you can filter (you need to take a look at my code) and the changes  i made
EE_Filter2.accdb
pdvsaProject finance

Author

Commented:

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 TsioumprisSoftware & Systems Engineer
Distinguished Expert 2019

Commented:
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.
pdvsaProject finance

Author

Commented:

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.