title: Query Excel worksheet in MS-Access VBA (using ADODB recordset)
body: <p>I'd like to query an Excel worksheet in VBA <strong>and</strong> specify conditions.</p>
  
  <p>The simple query <code>"SELECT * FROM [PCR$]"</code> works perfectly, but I don't know how to add a WHERE clause.</p>
  
  <p>I tried <code>cmd2.CommandText = "SELECT * FROM [PCR$] WHERE ([B1] IS NOT NULL)"</code> but then it complains about missing parameters.</p>
  
  <p>This is the complete code:</p>
  
  <pre><code>
  Dim rs2 As New ADODB.Recordset
  Dim cnn2 As New ADODB.Connection
  Dim cmd2 As New ADODB.Command
  Dim intField As Integer
  Dim strFile As String
  
  strFile = fncOpenFile
  If strFile = "" Then Exit Sub
  
  With cnn2
      .Provider = "Microsoft.Jet.OLEDB.4.0"
      .ConnectionString = "Data Source='" & strFile & "'; " & "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
      .Open
  End With
  
  Set cmd2.ActiveConnection = cnn2
  cmd2.CommandType = adCmdText
  cmd2.CommandText = "SELECT * FROM [PCR$]"
  rs2.CursorLocation = adUseClient
  rs2.CursorType = adOpenDynamic
  rs2.LockType = adLockOptimistic
  
  rs2.Open cmd2
  </code></pre>