home *** CD-ROM | disk | FTP | other *** search
- Sub cmdFilter_Click ()
- Const InitRecSource = "Company" ' The form's initial record source
- Dim Ctr As Integer ' A clause counter
- ReDim Clauses(20) ' An array to hold SQL clauses
- Dim FiltString As String ' Holds the built-up WHERE clause
- Dim SQL As String ' Holds SQL whilst itÆs built
- Dim i As Integer ' A general-purpose counter
-
- Ctr = 1
-
- ' With each filter control, we need to ascertain whether it is
- ' blank (null), and ignore it if it is; otherwise, we insert
- ' a corresponding WHERE sub-clause into the Clauses array.
- ' For simplicity, I have left the array fixed at 20 elements,
- ' allowing up to 20 filter values.
-
- ' Each string filter clause uses the LIKE operator with a trailing
- ' asterisk so that the user can get partial matches on the first
- ' few letters of the string
-
- If Not IsNull([Company]) Then ' Has a company been entered?
- Clauses(Ctr) = "[Company] LIKE '" & [Company] & "*'"
- Ctr = Ctr + 1 ' Count the new clause
- End If
-
- If Not IsNull([Address]) Then
- Clauses(Ctr) = "[Address] LIKE '" & [Address] & "*'"
- Ctr = Ctr + 1
- End If
-
- If Not IsNull([Post Code]) Then
- Clauses(Ctr) = "[Post Code] LIKE '" & [Post Code] & "*'"
- Ctr = Ctr + 1
- End If
-
- If Not IsNull([Phone]) Then
- Clauses(Ctr) = "[Phone] LIKE '" & [Phone] & "*'"
- Ctr = Ctr + 1
- End If
-
- If Not IsNull([Fax]) Then
- Clauses(Ctr) = "[Fax] LIKE '" & [Fax] & "*'"
- Ctr = Ctr + 1
- End If
-
- If Not IsNull([EMail]) Then
- Clauses(Ctr) = "[EMail] LIKE '" & [EMail] & "*'"
- Ctr = Ctr + 1
- End If
-
- If Not IsNull([Source]) Then
- Clauses(Ctr) = "[Source] LIKE '" & [Source] & "*'"
- Ctr = Ctr + 1
- End If
-
- If Tagged <> 0 Then
- Clauses(Ctr) = "[Tagged] = " & Choose(Tagged, "True", "False")
- Ctr = Ctr + 1
- End If
-
- FiltString = ""
- For i = 1 To Ctr - 1
- FiltString = FiltString & IIf(i > 1, " AND ", "") & Clauses(i)
- Next
-
- Me.recordsource = "" ' Remove the formÆs current record source
-
- If FiltString = "" Then ' If no filter criteria were specified
- Me.recordsource = InitRecSource ' Reset the record source..
- Me.caption = "Companies - ALL RECORDS" ' .. and the caption
- Else
- ' Build the SQL string. This might need to be more complex if
- ' we're filtering across multiple tables, possibly involving a
- ' sub-query
- SQL = "select * from [" & InitRecSource & "] "
- SQL = SQL + "where " & FiltString
-
- ' Set the new record source and caption for the form
- Me.recordsource = SQL
- Me.caption = "Companies & Contacts - FILTERED"
- End If
- End Sub
-