home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1995 October / PCPRO_OCT.ISO / code / database / code_b.txt
Encoding:
Text File  |  1995-08-08  |  2.9 KB  |  83 lines

  1. Sub cmdFilter_Click ()
  2.     Const InitRecSource = "Company"    ' The form's initial record source
  3.     Dim Ctr As Integer            ' A clause counter
  4.     ReDim Clauses(20)            ' An array to hold SQL clauses 
  5.     Dim FiltString As String        ' Holds the built-up WHERE clause
  6.     Dim SQL As String            ' Holds SQL whilst itÆs built
  7.     Dim i As Integer            ' A general-purpose counter
  8.  
  9.     Ctr = 1
  10.     
  11.     ' With each filter control, we need to ascertain whether it is
  12.     ' blank (null), and ignore it if it is; otherwise, we insert
  13.     ' a corresponding WHERE sub-clause into the Clauses array.
  14.     ' For simplicity, I have left the array fixed at 20 elements,
  15.     ' allowing up to 20 filter values.
  16.  
  17.     ' Each string filter clause uses the LIKE operator with a trailing
  18.     ' asterisk so that the user can get partial matches on the first
  19.     ' few letters of the string
  20.  
  21.     If Not IsNull([Company]) Then                                               ' Has a company been entered?
  22.         Clauses(Ctr) = "[Company] LIKE '" & [Company] & "*'"
  23.         Ctr = Ctr + 1    ' Count the new clause
  24.     End If
  25.  
  26.     If Not IsNull([Address]) Then
  27.         Clauses(Ctr) = "[Address] LIKE '" & [Address] & "*'"
  28.         Ctr = Ctr + 1
  29.     End If
  30.  
  31.     If Not IsNull([Post Code]) Then
  32.         Clauses(Ctr) = "[Post Code] LIKE '" & [Post Code] & "*'"
  33.         Ctr = Ctr + 1
  34.     End If
  35.  
  36.     If Not IsNull([Phone]) Then
  37.         Clauses(Ctr) = "[Phone] LIKE '" & [Phone] & "*'"
  38.         Ctr = Ctr + 1
  39.     End If
  40.  
  41.     If Not IsNull([Fax]) Then
  42.         Clauses(Ctr) = "[Fax] LIKE '" & [Fax] & "*'"
  43.         Ctr = Ctr + 1
  44.     End If
  45.  
  46.     If Not IsNull([EMail]) Then
  47.         Clauses(Ctr) = "[EMail] LIKE '" & [EMail] & "*'"
  48.         Ctr = Ctr + 1
  49.     End If
  50.  
  51.     If Not IsNull([Source]) Then
  52.         Clauses(Ctr) = "[Source] LIKE '" & [Source] & "*'"
  53.         Ctr = Ctr + 1
  54.     End If
  55.  
  56.     If Tagged <> 0 Then
  57.         Clauses(Ctr) = "[Tagged] = " & Choose(Tagged, "True", "False")
  58.         Ctr = Ctr + 1
  59.     End If
  60.  
  61.     FiltString = ""
  62.     For i = 1 To Ctr - 1
  63.         FiltString = FiltString & IIf(i > 1, " AND ", "") & Clauses(i)
  64.     Next
  65.     
  66.     Me.recordsource = ""                                                      ' Remove the formÆs current record source
  67.     
  68.     If FiltString = "" Then                                                      ' If no filter criteria were specified
  69.         Me.recordsource = InitRecSource                       ' Reset the record source..
  70.         Me.caption = "Companies - ALL RECORDS"      ' .. and the caption
  71.     Else
  72.         ' Build the SQL string. This might need to be more complex if
  73.       ' we're filtering across multiple tables, possibly involving a
  74.       ' sub-query
  75.         SQL = "select * from [" & InitRecSource & "] "
  76.         SQL = SQL + "where " & FiltString
  77.  
  78.         ' Set the new record source and caption for the form
  79.         Me.recordsource = SQL
  80.         Me.caption = "Companies & Contacts - FILTERED"
  81.     End If
  82. End Sub
  83.