This article was previously published under Q286828
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article applies only to a Microsoft Access project (.adp).
For a Microsoft Access 2000 version of this article, see 235359.
This article shows you how to use a Microsoft Access form to specify the query criteria in an Access project.
In an Access database (.mdb) or in a Microsoft Office Access 2007 database (.accdb), you can use the query by form (QBF) technique to create a "query form" in which you enter criteria. The query form contains blank text boxes or combo boxes, each representing a field in your Access table that you want to query. You make entries only in the boxes for which you want to specify search criteria.
You can also implement the QBF technique in an Access project. In an Access project, you would use a stored procedure to find the records that you want to view, and then create an additional form to display the output of the stored procedure, as follows.
Creating a Stored Procedure
Follow these steps to create a stored procedure that you will use to return data to a form:
Open the sample Access project NorthwindCS.adp. By default, this file is installed in the <Drive>:\<Microsoft Access samples folder>.
In Microsoft Office Access 2003 or in Microsoft Access 2002, click Query on the Insert menu.
In Microsoft Office Access 2007, click the Create tab.
In Access 2003 or in Access 2002, click Create Text Stored Procedure in the New Query dialog box, and then click OK.
In Access 2007, click Stored Procedure in the Other group.
Type the following Transact-SQL statements in the Stored Procedure window:
Create Procedure "QBFProc" @CustomerID varchar(10), @EmployeeID int, @OrderDate datetime As Declare @SQLSTRING varchar(1000) Declare @SelectList varchar(1000) SET NOCOUNT ON SELECT @SelectList = 'SELECT * FROM Orders' --Check to see if CustomerID search criteria is NULL. --If it contains a value, Begin to construct a WHERE clause. IF @CustomerID Is NOT NULL BEGIN SELECT @SQLString = 'WHERE CustomerID = ''' + @CustomerID + '''' END --Check to see if EmployeeID search criteria is NULL. --If it contains a value, add additional information to the WHERE clause. IF @EmployeeID Is NOT NULL BEGIN IF @SQLSTRING Is NOT NULL BEGIN SELECT @SQLSTRING = @SQLSTRING + ' AND EmployeeID = ' + Convert(varchar(100), @EmployeeID) END ELSE BEGIN SELECT @SQLSTRING = 'WHERE EmployeeID = ' + Convert(varchar(100), @EmployeeID) END END --Check to see if OrderDate search criteria is NULL. --If it contains a value, add additional information to the WHERE clause. IF @OrderDate Is NOT NULL BEGIN IF @SQLSTRING Is NOT NULL BEGIN SELECT @SQLSTRING = @SQLSTRING + ' AND OrderDate = ''' + Convert(varchar(20), @OrderDate) + '''' END ELSE BEGIN SELECT @SQLSTRING = 'WHERE OrderDate = ''' + Convert(varchar(20), @OrderDate) + '''' END END --Concatenate the SELECT and the WHERE clause together --unless all three parameters are null in which case return --@SelectList by itself without criteria. IF @CustomerID IS NULL AND @EmployeeID IS NULL AND @OrderDate IS NULL BEGIN SELECT @SelectList = @SelectList END ELSE BEGIN SELECT @SelectList = @SelectList + ' ' + @SQLSTRING END --Execute the SQL statement. EXECUTE(@SELECTLIST)
Save the stored procedure with the default name of QBFProc, and then close the procedure.
Creating the Query By Form
Follow these steps to create the QBF_Form form, which you will use to choose search criteria used by the stored procedure.
Create a new form that is not based on any table or query, and name it QBF_Form.
In Access 2003 or Access 2002, make sure that the Control Wizards button is not pressed in in the toolbox. In Access 2007, make sure that the Use Control Wizards button is not pressed in the Controls group. Then, add three combo boxes to the form with the following property assignments:
To use the sample that you just created, open the QBF_Form form, and select values in any or all of the combo boxes. Click the command button to open the frmFinal form, which will display any matching records that meet the criteria that you selected in the QBF_Form form.
For additional information about QBF in an Access database, click the following article number to view the article in the Microsoft Knowledge Base:
209645 ACC2000: How to Use the Query-by-Form (QBF)Technique
For additional information about Transact-SQL (TSQL) and creating stored procedures with input parameters, visit the following Microsoft Web site: