Form or report that is bound to a stored procedure or a function does not apply the WHERE condition (275089)
The information in this article applies to:
- Microsoft Office Access 2003
- Microsoft Access 2002
This article was previously published under Q275089 Moderate: Requires basic macro, coding, and interoperability skills.
This article applies only to a Microsoft Access project (.adp).
SYMPTOMS
When you try to filter an Access form or report by using a value that is displayed on another object, such as another Access form, the filter is not applied.
RESOLUTION
There are several methods that one could use to resolve the inability to filter a form or a report that is bound to a stored procedure or function. Most of these resolutions involve replacing the stored procedure or the function with some other type of record source, such as a table, a view, or an SQL statement.
However, the following resolution shows you how you can continue to use a stored procedure as the record source for a form while successfully implementing a WHERE condition or filter. CAUTION: If you follow the steps in this example, you modify the sample Access project NorthwindCS.adp. You may want to back up the NorthwindCS.adp file and follow these steps on a copy of the project. - Open the sample project NorthwindCS.adp.
- In the Database window, click to highlight the Categories table, and then click AutoForm on the Insert menu.
- Open the form in Design view.
- Delete the subform object that is labeled Table.Products from the main form, and then add a command button to the form. If the wizard starts, click Cancel.
- Set the following properties for the command button:
Name: cmdFilterProducts
Caption: Filter Products
On Click: [Event Procedure]
- Set the OnClick property of the command button to the following event procedure:
Private Sub cmdFilterProducts_Click()
DoCmd.OpenForm "Products", acNormal, , , acFormEdit, acWindowNormal
End Sub
- On the File menu, click Close and Return to Microsoft Access.
- On the File menu, click Save, and save the form as Categories1.
- In the Database window, click Queries, and then click New.
- In the New Query dialog box, click Create Text Stored Procedure, and then click OK.
- Type or paste the following Transact-SQL statement, and then close and save the procedure with the default name of spProducts:
CREATE PROCEDURE spProducts
@CatID int
AS
SELECT * FROM Products WHERE CategoryID = @CatID
RETURN
- Open the Products form in Design view, and then change the Record Source property from Products to spProducts.
- Scroll down to the InputParameters property of the form, and then assign the following value to this property:
@CatID int = Forms![Categories1]![CategoryID]
- Close and save the Products form, and then open the Categories1 form.
- Browse to the CategoryName Confections, and then click the Filter Products button. Note that the Products form opens and displays only those products where Category is equal to Confections.
STATUS
This behavior is by design.
| Modification Type: | Minor | Last Reviewed: | 6/7/2004 |
|---|
| Keywords: | KbVBA kbProgramming KbClientServer kbnofix kbprb KB275089 |
|---|
|