title: How to filter a form with multi-Field in MS Access using VBA
body: <p>I have the code below in a click button event in MS Access 2013 form, whose datasource is from a query with the following fields “EnrNo, FirstName, LastName, and RegDate”.</p>
  
  <p>The form has three text boxes and a command button:</p>
  
  <ol>
  <li>txtKeyword</li>
  <li>txtDateFrom</li>
  <li>txtDateTo</li>
  <li>cmdSearch</li>
  </ol>
  
  <p>The click button (cmdSearch) in the form is intended to filter the query based on three criteria’s , which could be any of the two “EnrNo, FirstName, LastName” AND the range of the date(s) entered in the text box txtDateTo and cmdSearch</p>
  
  <p>My code successfully filters only EnrNo. Please help me out... Thanks for your time.</p>
  
  <pre><code>Private Sub cmdSearch_Click()
  Dim strWhere As String                  'The criteria string.
  Dim Where As String
  Dim lngLen As Long                      'Length of the criteria string   to append to.
  Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.
  
  'Text field example. Use quotes around the value in the string.
  If Not IsNull(Me.txtFilter) Then
  strWhere = strWhere &amp; "([EnrNo] = """ &amp; Me.txtFilter &amp; """) OR "
  strWhere = strWhere &amp; "([FirstName] = """ &amp; Me.txtFilter &amp; """) AND "
  End If
  
  'Date field example. Use the format string to add the # delimiters and get the right international format.
  If Not IsNull(Me.txtFrom) Then
  strWhere = strWhere &amp; "([RegDate] &gt;= " &amp; Format(Me.txtFrom, conJetDate) &amp; ") AND "
  End If
  
  'Another date field example. Use "less than the next day" since this field has times as well as dates.
  If Not IsNull(Me.txtTo) Then   'Less than the next day.
  strWhere = strWhere &amp; "[RegDate] BETWEEN #" &amp; Format(Me.txtFrom, "mm/dd/yyyy") &amp; "# AND #" &amp; Format(Me.txtTo, "mm/dd/yyyy") &amp; "# "
  End If
  
  lngLen = Len(strWhere) - 5
  If lngLen &lt;= 0 Then     'Nah: there was nothing in the string.
      MsgBox "No criteria", vbInformation, "Nothing to do."
  Else                    'Yep: there is something there, so remove the "  AND " at the end.
      strWhere = Left$(strWhere, lngLen)
    'Debug.Print strWhere
  
  Me.Filter = strWhere
  Me.FilterOn = True
  End If
  End Sub
  </code></pre>