Microsoft KB Archive/266654

= How To Implement Dynamic Search Functionality to Locate Records in a DataGrid That Is Bound to a Large ADO Recordset =

Article ID: 266654

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft ActiveX Data Objects 2.1
 * Microsoft ActiveX Data Objects 2.1 Service Pack 1
 * Microsoft ActiveX Data Objects 2.1 Service Pack 2
 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7

-



This article was previously published under Q266654



SUMMARY
Many Visual Basic database applications require that you locate record(s) that match a specified criteria in a DataGrid control that is bound to a large ActiveX Data Objects (ADO) recordset. To simplify the process of scrolling through the grid to locate the necessary record(s), you can sort the bound recordset by a lookup field rather than searching through an unsorted recordset.

A more sophisticated method to address this requirement is to dynamically restrict the records that are displayed in the bound DataGrid to those records that contain a lookup value that the user types for a chosen filter column in the recordset. The code sample in this article demonstrates how to enforce a filter on the recordset to restrict the records according to this method.



Step-by-Step Example
This example uses the data from the SQL Server PUBS sample database. You can also bind the DataGrid to a recordset based on one of your own databases if SQL Server is not installed or if you do not have the PUBS sample database.  Create a new Standard EXE project in Visual Basic. Form1 is created by default. From the Project menu, click Reference, and then click Microsoft ActiveX Data Objects Library. From the Project menu, click Components. Add the ADO DataControl control and the OLE DB DataGrid control to the toolbox. Drag a TextBox control, an OLE DB DataGrid, and an ADO DataControl, and drop these controls onto Form1. Set the properties of the ADO DataControl to bind it to the Authors table in the SQL Server PUBS sample database. Set the properties of the OLE DB DataGrid to bind it to the ADO DataControl, and size it to display no more than five records at a time. Set the Text property of the TextBox to a blank string, and name the control txtLName.

NOTE This example uses the au_lname column in the Authors table as the lookup field to locate matching records in the DataGrid. At run time, the user can type a lookup value for the au_lname column in the TextBox. As the user types a string of characters to specify an author's last name, this value is used to dynamically filter and display the matching records in the DataGrid. Each key stroke is trapped and used to construct a string, which is used as the filter pattern to locate matching records.</li>  To implement this functionality, copy and paste the following code in the Change event of txtLName: Dim mname As String mname = Trim(txtLName.Text) If mname <> &quot;&quot; Then Adodc1.Recordset.Filter = &quot;au_lname like '&quot; & mname & &quot;%'&quot; Else Adodc1.Recordset.Filter = &quot;au_lname <> '123'&quot; End If                   </li> Save the project. Press the F5 key to run the project. Initially, all the records in the Authors table are displayed in the DataGrid.</li> To test the dynamic search functionality, type a string of characters into the TextBox txtLName to locate matching records. As you type each character, the grid is updated to display only the records whose au_lname begins with the string that you type in the TextBox.</li></ol>

A string pattern that does not have any matching records results in a blank grid. If an entry is blank in the TextBox, the grid is restored to its initial state, which displays all the records in the Authors table. Notice the filter criterion that is used to restore the grid to its initial state. The adFilterNone constant is not used because the OLE DB DataGrid does not refresh correctly when one of the ADO FilterGroupEnum constants are specified as the filter criteria for the bound recordset. For additional information about this limitation of the DataGrid control, click the article number below to view the article in the Microsoft Knowledge Base:

191919 BUG: DataGrid Doesn't Handle Special Filter Constants Correctly

<div class="references_section">