Combo Filter list in Access

D

Donuts

Hi,

I'm a beginner in Access VBA and I need help in writing a code for
combo filter boxes which are inserted into one form - ie. Combo boxe
are City and Country. The aim is to allow a user to filter either th
combo filter by a. EITHER or b. BOTH combo boxes. I've managed to ge
one of combo boxes to filter in VBA code , but does anyone know how
can get the other filter form to work too?


The code I have written for the City combo filter box is:

strfilter = CBOCity.Value
Set Citizens = Reports("Destination")
With Citizens
.Filter = "[City] = " & "'" & strfilter & "'"
.FilterOn = True Or False

Rgds

Donuts
 
G

Gerald Stanley

Try something along the following lines

1. On the Form's VB page, create a private sub ApplyFilter
as follows

Private Sub ApplyFilter()
Dim strFilter As String

strFilter = ""
If cmbCity.Value <> "" Then
strFilter = "City = " & cmbCity.Value 'Assuming the
column in your table is called City
End If
If cmbCountry.Value <> "" Then
If strFilter = "" Then
strFilter = "Country = " & cmbCountry.Value
'Assuming the column inyour table is called Country
Else
strFilter = "AND Country = " & cmbCountry.Value
End If
End If
DoCmd.ApplyFilter , strFilter
End Sub

2. In the AfterUpdate methods of each of the combo boxes,
place a call to ApplyFilter e.g.

Private Sub cmbCity_AfterUpdate()
ApplyFilter
End Sub

Private Sub cmbCountry_AfterUpdate()
ApplyFilter
End Sub

Hope That Helps
Gerald Stanley MCSD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top