Link to home
Start Free TrialLog in
Avatar of pdvsa
pdvsaFlag 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.
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

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

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


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

Avatar of pdvsa

ASKER

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

EE_Filter2.accdb

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.

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

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


Avatar of pdvsa

ASKER

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

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.



I am here...:) what exactly is the issue?
ASKER CERTIFIED SOLUTION
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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

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