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.
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.
- 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:
OnClick: Event Procedure
- 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 = " Where "
'Start the first part of the select statement.
sSQL = "select * from customers "
'Loop through each control on the form to get its value.
For Each ctl In Me.Controls
'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
'This is the function that actually builds
If sWhereClause = " Where " Then
sWhereClause = sWhereClause & BuildCriteria(.Name, dbtext, .Text)
sWhereClause = sWhereClause & " and " & BuildCriteria(.Name, dbtext, .Text)
'Set the forms recordsource equal to the new
Me.txtSQL = sSQL & sWhereClause
Me.RecordSource = sSQL & sWhereClause
- Save the form, and then open it in Form view.
Article ID: 304302 - Last Review: Oct 30, 2008 - Revision: 1