How to filter Form Using Several Comboboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bullfrog83
    New Member
    • Apr 2010
    • 124

    How to filter Form Using Several Comboboxes

    In the form header I have four unbound comboboxes that I use to filter records in the detail section. You can select a value in one, two, three or all four of the comboboxes and it doesn't matter the order. Basically, I'm trying to duplicate the filtering feature in excel. Each combobox allows an "All" value (0 being the All's id). On the AfterUpdate event of each filter combobox I only have "Me.Requery " since I have the filtering work done in my form's OnCurrent event which is the following:

    Code:
        Dim strFilterMatricYT As String
        Dim strFilterCollege As String
        Dim strFilterProgram As String
        Dim strFilterDegree As String
        Dim strAnd1 As String
        Dim strAnd2 As String
        Dim strAnd3 As String
        
        strFilterMatricYT = IIf(Me.cboFilterMatricYT = 0, "", "DegReq.MatricId = " & Me.cboFilterMatricYT)
        strFilterCollege = IIf(Me.cboFilterCollege = 0, "", "DegReq.CollegeId = " & Me.cboFilterCollege)
        strFilterProgram = IIf(Me.cboFilterProgram = 0, "", "DegReq.ProgramId = " & Me.cboFilterProgram)
        strFilterDegree = IIf(Me.cboFilterDegree = 0, "", "DegReq.DegreeId = " & Me.cboFilterDegree)
        strAnd1 = IIf(strFilterMatricYT <> "" And strFilterCollege <> "", " AND ", "")
        strAnd2 = IIf((strFilterMatricYT <> "" Or strFilterCollege <> "") And strFilterProgram <> "", " AND ", "")
        strAnd3 = IIf((strFilterMatricYT <> "" Or strFilterCollege <> "" Or strFilterProgram <> "") And strFilterDegree <> "", " AND ", "")
            
        If strFilterMatricYT = "" And strFilterCollege = "" And strFilterProgram = "" And strFilterDegree = "" Then
            Me.FilterOn = False
        Else
            Me.FilterOn = True
            Me.Filter = strFilterMatricYT & strAnd1 & strFilterCollege & strAnd2 & strFilterProgram & strAnd3 & strFilterDegree
        End If
    This code works. Each strFilter variable accommodates the All selection and I have 3 strAnd variables to piece together the strFilter variables if more than one has a value selected. My question: Is there an easier, cleaner, or more efficient way to do this? Again, I'm trying to duplicate the filtering feature in excel. Thanks!
    Last edited by bullfrog83; May 26 '10, 03:13 PM. Reason: Added one more thing
  • Megalog
    Recognized Expert Contributor
    • Sep 2007
    • 378

    #2
    Not sure if this is cleaner or more efficient, but it may be easier to modify down the road if you need to drop another filter box onto the form.

    Code:
    Dim strFilter As String
    
    If Me.cboFilterMatricYT <> 0 Then
        strFilter = "DegReq.MatricId = " & Me.cboFilterMatricYT
    End If
    
    If Me.cboFilterCollege <> 0 Then
        If strFilter = "" Then
            strFilter = "DegReq.CollegeId = " & Me.cboFilterCollege
        Else
            strFilter = strFilter & " AND DegReq.CollegeId = " & Me.cboFilterCollege
        End If
    End If
    
    If Me.cboFilterProgram <> 0 Then
        If strFilter = "" Then
            strFilter = "DegReq.ProgramId = " & Me.cboFilterProgram
        Else
            strFilter = strFilter & " AND DegReq.ProgramId = " & Me.cboFilterProgram
        End If
    End If
    
    If Me.cboFilterDegree <> 0 Then
        If strFilter = "" Then
            strFilter = "DegReq.DegreeId = " & Me.cboFilterDegree
        Else
            strFilter = strFilter & " AND DegReq.DegreeId = " & Me.cboFilterDegree
        End If
    End If
    
    Me.Filter = strFilter
    Me.FilterOn = True

    Comment

    Working...