cookiemonster
cookiemonster

Reputation: 368

Filter form based on two combo box selections

I have this two combobox on my form. Each one filters the form independently. but I want to filter the form based on both selections.

I have this code to a check box after update event to filter the form with the two combo box but it doesn't work:

combo19 is the name for the first combo box and combo21 is the name for the second combobox.

Private Sub Check34_AfterUpdate()
 Me.Filter = Me.Combo19 & Me.Combo21
 Me.FilterOn = True
 Me.Refresh

End Sub

Upvotes: 0

Views: 2348

Answers (1)

Eperbab
Eperbab

Reputation: 378

The Filter property is a string expression consisting of a WHERE clause without the WHERE keyword. For example, the following Visual Basic code defines and applies a filter to show only customers from the USA:

Me.Filter = "Country = 'USA'" 
Me.FilterOn = True

https://msdn.microsoft.com/en-us/vba/access-vba/articles/form-filter-property-access

In your case, it would look like:

Me.Filter = "field1 = '" & Me.Combo19 & "'" & " AND field2 = '" & Me.Combo21 & "'"  

Where Field1 and Field2 should be replaced with the actual column names in your recordsource.
Also, your code doesn't check if Combo19 or Combo21 is empty.

Upvotes: 1

Related Questions