ADO Samples

Filter and RecordCount Properties Example (JScript)

This example opens a Recordset on the Companies table of the Northwind database and then uses the Filter property to limit the records visible to those where the CompanyName field starts with the letter D.

<!-- BeginFilterJS -->
<%@  Language=JavaScript %>
<!-- Include file for JScript ADO Constants -->
<!--#include File="adojavas.inc"-->

<html>

<head>
<title>ADO Recordset.Filter Example</title>
<style>
<!--
BODY {
   font-family: 'Verdana','Arial','Helvetica',sans-serif;
   BACKGROUND-COLOR:white;
   COLOR:black;
    }
.thead {
   background-color: #008080; 
   font-family: 'Verdana','Arial','Helvetica',sans-serif; 
   font-size: x-small;
   color: white;
   }
.thead2 {
   background-color: #800000; 
   font-family: 'Verdana','Arial','Helvetica',sans-serif; 
   font-size: x-small;
   color: white;
   }
.tbody { 
   text-align: center;
   background-color: #f7efde;
   font-family: 'Verdana','Arial','Helvetica',sans-serif; 
   font-size: x-small;
    }
-->
</style>
</head>

<body bgcolor="White">

<h1>ADO Recordset.Filter Example</h1>
<!-- Page text goes here -->
<%
   myConnect = "Provider=sqloledb;Data Source=" +
      Request.ServerVariables("SERVER_NAME") + ";" +
      "Initial Catalog=Northwind;User Id=sa;Password=;";
   mySQL = "select * from Customers;";
   showBlank = " ";
   showNull = "-NULL-";
   
   var connTemp = Server.CreateObject("ADODB.Connection");
   connTemp.Open(myConnect);
   rsTemp = Server.CreateObject("ADODB.Recordset");
   rsTemp.ActiveConnection = connTemp;
   rsTemp.CursorLocation = adUseClient;
   rsTemp.CursorType = adOpenKeyset;
   rsTemp.LockType = adLockOptimistic;
   rsTemp.Source = mySQL;
   rsTemp.Open();
   
   rsTemp.MoveFirst();
   rsTemp.Filter = "CompanyName like 'd*'";
   
   if (rsTemp.RecordCount < 1) {
      Response.Write("No records matched ");
      Response.Write (mySQL + "So cannot make table...");
      connTemp.Close();
      Response.End
   } else {
      Response.Write('<table width="100%" border="2">');
      Response.Write('<tr class="thead2">');
      // Put Headings On The Table for each Field Name
      for (var i=0; i<rsTemp.Fields.Count; i++) {
         var fieldObject = rsTemp.Fields(i);
         Response.Write('<th width="' + Math.floor(100 / rsTemp.Fields.Count) + '%">' + fieldObject.Name + "</th>");
      }
      Response.Write("</tr>");
      
      while(!rsTemp.EOF) {
         Response.Write('<tr class="tbody">');
         for (var thisField=0; thisField<rsTemp.Fields.Count; thisField++) {
            fieldObject = rsTemp.Fields(thisField);
            strField = fieldObject.Value;
            if (strField == null)
               strField = shownull;
            if (strField == "")
               thisField=showblank;
            Response.Write("<td>" + strField + "</td>")
         }
         rsTemp.MoveNext();
         Response.Write("</tr>");
      }
      Response.Write("</table>");
   }
   
   rsTemp.Close();
   connTemp.Close();
%>

</body>

</html>
<!-- EndFilterJS -->

See Also

Filter Property | RecordCount Property | Recordset Object