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:
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!
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
Comment