Link to home
Start Free TrialLog in
Avatar of Sbovino
SbovinoFlag for United States of America

asked on

Ms Access - How to FIlter a combo box on Field Data

I have a task to clean up a data set.  I have one table (b) that has an employee  id and a combined last name, first name field.  Another table (a) contains other data along with a first name and last name field.  I am using a combo box to do a look-up by name and auto populate the other fields.  Is it possible to build a combo box that will filter the combo name field LIKE the last name field in the form?

In other words my task is to assign employee ids to all employees listed in table A. I thought the task might be easier if there a way to filter the combo box so that it displays names that begin with last  name that is displayed on the form.  If  Robinson is the name in the last name field can I filter the combo box to select data that appears as Robinson, John.....Robinson, Mary in the look up table.  Possible?
Avatar of Bitsqueezer
Bitsqueezer
Flag of Germany image

Hi,

it's not completely sure at what conditions you want to filter. But in general you can always reload a list of a combobox to show a new contents. For example, you can use the "AfterUpdate" event of the last name field to set a new rowsource for the combobox by creating a SQL command which only loads the wanted records. For example:

SELECT ID, LastFirstName FROM tblB WHERE LastFirstName LIKE "*" & Nz(Forms!YourFormName!LastNameField) & "*"

Open in new window


You only need to adjust the SQL command to fit your needs.

Cheers,

Christian
Avatar of Sbovino

ASKER

I see the concept.  If I understand correctly I will use the afterUpdate event of the last name field to to set a new row source for the combo.  I am not seeing how this SQL command attaches to the combo query.  Thanks!
Hi,

I cannot say that exactly from your question, it would be easier to have a little example. But the concept would be: If you change something in one field then an event tells the other field to update it's list. This method would also apply to examples where you have combined comboboxes (like selecting a country first, then the city of the chosen country) or if you enter something in a field and while entering, change the list of results (like the dropdown in Google for example), here it would be the Change event of the combobox.

Cheers,

Christian
Avatar of Sbovino

ASKER

Christian:

Going back to your original post, the first thing I tried was altering the SQL statement that the combo uses with your suggestion...  This worked on the first record.  When I move to a new record all my attempts at forcing the combo to requery have failed.  In other words, using your SQL when I enter the first record the combo box filtered to like names.  Moving on to the new record the combo box results from the previous record remained despite my coding several events to force the requery.  Any suggestions on what event would force a requery so that when I arrive at the next record and click on the combo box it requries?  Many thanks.
Hi,

you would need to reset the SQL command back to the original one (in other words: In the example above without the "WHERE" part). The best place to do that is to use the Form_Current event.

Moreover you should keep in mind that Access saves the setting of the combobox each time you change to design mode and save the form. Means: If you have the original SQL command without the "WHERE" part as default in the combobox and you entered something in the form which causes the SQL command to be changed like in the example above and you then go back to form design mode, the SQL command WITH the WHERE-part is still in the combobox RowSource and if you then save the form this will be saved, too.

If you reset that in the Form_Current event this will be done also when the form loads initially as this event fires after Form_Load when the first record is displayed.

Cheers,

Christian
Avatar of Sbovino

ASKER

Christian

I am using this code in the Form_curent event.  

Dim strsql As String
strsql = "SELECT [Emp List].Name, [Emp List].Location, [Emp List].[Emp ID] " & vbCrLf & _
"FROM [Emp List] " & vbCrLf & _
"ORDER BY [Emp List].Name;"
Me.Combo14.RowSource = strsql
Me.Combo14.Requery

Now when I move to a new record I am not filtering per the original  SQL in the combo box that follows your example (like)  

Hoping you can point me in the right direction.
Avatar of Sbovino

ASKER

Solved this.  The query in the combo box is as you suggested.  You saw the code above which is working.  What i did was to put code in the on enter event of the combo box changing the SQL statement to the version with the where clause.  Seems to work just fine although I don't know that this is the best way to do it.  When I enter the combo it only has filtered selections based on the last name field on the form.  Thanks,

Steve
ASKER CERTIFIED SOLUTION
Avatar of Bitsqueezer
Bitsqueezer
Flag of Germany 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 Sbovino

ASKER

Concise and clear assistance.  Thank you for your help an suggestions.