Microsoft KB Archive/304302

= How to build a dynamic query with values from a search form in Access 2007, in Access 2003, or in Access 2002 =

Article ID: 304302

Article Last Modified on 3/28/2007

-

APPLIES TO


 * Microsoft Office Access 2007
 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q304302



Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to either a Microsoft Access database (.mdb) file or to a Microsoft Access database (.accdb) file, and to a Microsoft Access project (.adp) file.



SUMMARY
This article shows you how to dynamically build criteria for a query string with values from a search form in Microsoft Office Access 2007, in Office Access 2003, and in Microsoft Access 2002.



MORE INFORMATION
Sometimes, you may want to create a form that serves as a search form. You want to be able to enter values on the form and dynamically build the appropriate SQL string. The following steps show you how to dynamically build a query string that uses the BuildCriteria method.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

Step-by-Step Example
 Start Access. On the Help menu, point to Sample Databases, and then click Northwind Sample Database. Open the Customers form in Design view.  Add a command button and a text box to the form, and then set the following properties:   Command Button Name: cmdSearch Caption: Search OnClick: Event Procedure

Text Box --  Name: txtSQL Width: 4.4583&quot; Height: 1.25&quot;   Set the OnClick property of the command button to the following event procedure: Private Sub cmdSearch_Click On Error Resume Next Dim ctl As Control Dim sSQL As String Dim sWhereClause As String 'Initialize the Where Clause variable. sWhereClause = &quot; Where &quot; 'Start the first part of the select statement. sSQL = &quot;select * from customers &quot; 'Loop through each control on the form to get its value. For Each ctl In Me.Controls With ctl 'The only Control you are using is the text box. 'However, you can add as many types of controls as you want. Select Case .ControlType Case acTextBox .SetFocus 'This is the function that actually builds 'the clause. If sWhereClause = &quot; Where &quot; Then sWhereClause = sWhereClause & BuildCriteria(.Name, dbtext, .Text) Else sWhereClause = sWhereClause & &quot; and &quot; & BuildCriteria(.Name, dbtext, .Text) End If           End Select End With Next ctl 'Set the forms recordsource equal to the new 'select statement. Me.txtSQL = sSQL & sWhereClause Me.RecordSource = sSQL & sWhereClause Me.Requery End Sub  Save the form, and then open it in Form view.</li></ol>

Note that when you click the Search command button, the txtSQL text box reflects the query that was created from the values on the Customers form. Also, the Customers form has been requeried so that it reflects the results of the new SQL string.

<div class="references_section">