Microsoft KB Archive/278400

= How to use form parameters in the RecordSource property in an Access project =

Article ID: 278400

Article Last Modified on 4/5/2007

-

APPLIES TO


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

-



This article was previously published under Q278400



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

This article applies only to a Microsoft Access project (.adp).



SUMMARY
This article describes how to use the Forms! ! parameter reference in the RecordSource property for a form in a Microsoft Access project (ADP). In this method, the difficult step is integrating a server-based query with Access objects, such as a form reference.



MORE INFORMATION
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.

The following steps create a command button on the Customers form that opens the Orders form and shows only the matching records for the current customer.

Add a command button for the Customers form

 * 1) In Access, open the sample database NorthwindCS.adp.
 * 2) Open the Customers form in Design view.
 * 3) Using the wizard, add a command button to the Customers form that opens the Orders form and returns all records.
 * 4) Close and save the form.

Create a function to return matching records
 In the Queries object group, double-click Create function in designer.

Note In Access 2007, click Query Wizard in the Other group on the Create tab. In the New Query dialog box, click Design In-Line Function, and then click OK. In the Add Table dialog box, click the Views tab, click Orders Qry, and then click OK. In the Orders Qry object, click to select the * (All Columns) check box. Add the CustomerID field to the function. Add = @p1 to the criteria for the CustomerID field. Click to clear the Output property for the CustomerID field.

Notice that theCustomerID field is automatically displayed because the * (All Columns) option was selected.</li>  On the View menu, point to Show Panes, and then click SQL.

Note In Access 2007, click SQL in the Tools group on the Design tab.

Notice that the following SQL statement should be displayed in the SQL pane. SELECT dbo.[Orders Qry].*<BR/> FROM dbo.[Orders Qry]<BR/> WHERE (CustomerID = @p1) </li> Close and save this function as fn_CustOrders .</li></ol>

Modify Orders form to show matching records
<ol> Open the Orders form in Design view.</li>  Modify the RecordSource property to set the parameter from the function to use the CustomerID field from the Customers form as criteria. Select * from fn_CustOrders(@[Forms]![Customers]![CustomerID]) Notice that the Input Parameter property has been automatically changed to: ? = [Forms]![Customers]![CustomerID] </li> Close and save the Orders form.</li></ol>

Test the Customers and the Orders forms

 * 1) Open the Customers form.

Notice which customer appear.
 * 1) Click the command button to open the Orders form.

Notice that only those orders matching the current customer appear.

Using this approach allows the records to be filtered on the server side, based upon which customer is displayed, returning only the matching records for the current customer.

Additional query words: OfficeKBHowTo inf ACC2002 ACC2003 ACC2007

Keywords: kbexpertiseinter kbinfo kbclientserver kbhowto KB278400

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.