asked on
How to filter a combo box on a form?
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.
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.
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.
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 ...
ASKER
HI Gustav, I accidentally attached the wrong db. So sorry about that. I posted it below.
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.
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.
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.
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.
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
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.
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.
e.g your combobox has a controlSource :
Open in new window
if you want to filter itOpen in new window
orOpen in new window