Link to home
Start Free TrialLog in
Avatar of Glen_D
Glen_D

asked on

How do I link combo boxes to select query in a form?

I have three combo boxes each with its own query.  I want to filter a data set from the main table using these combo boxes; the combo boxes are basically filters (parameters) that the user selects to filter his or her data.

How do I do this?

Thank You
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

you can use something like this


Option Explicit
Dim sFilter As String
Sub ApplyFilter()
    sFilter = ""
    If Me.cboCustomer <> "" And Not IsNull(Me.cboCustomer) Then
        If sFilter = "" Then
            sFilter = "[CustID]= " & Me.cboCustomer
        Else
            sFilter = sFilter & " and [CustID]= " & Me.cboCustomer
        End If
    End If
   
    If Me.cboEmployee <> "" And Not IsNull(Me.cboEmployee) Then
        If sFilter = "" Then
            sFilter = "[EmployeeID]= " & Me.cboEmployee
        Else
            sFilter = sFilter & " And [EmployeeID]= " & Me.cboEmployee
        End If
    End If

Forms!NameOfmainForm!NameOfSubform.Filter=sFilter
Forms!NameOfmainForm!NameOfSubform.FilterOn=True
Forms!NameOfmainForm!NameOfSubform.Refresh

end sub


private sub cboEmployee_afterupdate()

applyFIlter

end sub


private sub cboCustomer_afterupdate()

applyFIlter

end sub

Open in new window

Avatar of Glen_D
Glen_D

ASKER

Thanks...let me give this a try as I have to rename the code tables
Avatar of Glen_D

ASKER

I'm getting a bit stuck here..

The 2 forms names are:

osewg_main
osewg_sub

Combo boxes have queries (select distinct challenges from osewg_main order by challenges)

combo 1 - lu_challenges
combo 2 - lu_heritage
combo 3 - lu_instrument

Could you please guide me a bit more?  I know I need to open the code window, paste and modify but I can't seem to get the combo linked. I am using a form and subform.

Thank You

can you upload a copy of the your db?
Avatar of Glen_D

ASKER

yes....
Pays-DB.mdb
you forgot to include your subform.
Avatar of Glen_D

ASKER

Yes....sorry ...was cleaning...hold on...
and add more records... ;-)
Avatar of Glen_D

ASKER

I included the queries for the combo boxes...only two in this test version and the sub form
Avatar of Glen_D

ASKER

test db
Pays-DB.mdb
Avatar of Glen_D

ASKER

more records

thx
Pays-DB.mdb
how do you want to do the search based on the selection from the combo box?
after the click event of a command button?
Avatar of Glen_D

ASKER

I was looking for the after click in each successive combo box..

If I only selected the top combo, only records with that filter would be displayed
If I selected both combo boxes, then filtered data for both combo boxes would be displayed

The resultant data set should probably be a in a data form but I can adjust later..

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America 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 Glen_D

ASKER

very nice....awesome support.

Thank You so much; I'll be studying your code so I can build off this later.

Glen