Microsoft KB Archive/827007

= How to work with stored procedures by using script in an InfoPath form =

Article ID: 827007

Article Last Modified on 3/23/2007

-

APPLIES TO


 * Microsoft Office InfoPath 2007
 * Microsoft Office InfoPath 2003, Service Pack 1 (SP1)
 * Microsoft Office InfoPath 2003

-



SUMMARY
This article describes step-by-step instructions for creating a Microsoft Office InfoPath 2007 form or a Microsoft Office InfoPath 2003 form that works with parameterized stored procedures.



MORE INFORMATION
The top-level XDocument object for InfoPath exposes the QueryAdapter property. The QueryAdapter property returns a reference to the data adapter object that is associated with an InfoPath form. At run time, you can use the QueryAdapter property to access the primary data source of your form and to then run commands. You can use the QueryAdapter property to run a stored procedure that returns records or submits records on your form. This is described in the following sections.

Create a new form from a data source
 Start InfoPath. In InfoPath 2003, follow these steps:  On the File menu, click Design a Form. In the Design a Form task pane, click New from Data Source.

The Data Source Setup Wizard starts. Choose Database for the data source, and then click Next. Click Select Database.

The Select Data Source dialog box appears. Click New Source.

The Data Connection Wizard appears.</li></ol>

In InfoPath 2007, follow these steps: <ol style="list-style-type: lower-alpha;"> On the File menu, click Design a Form Template.</li> In the Design a Form Template window, click Blank, and then click OK.</li> On the Tools menu, click Data Connections, and then click Add.

The Data Connection Wizard appears.</li> Click to select Create a new connection to, click to select Receive data, and then click Next.</li></ol> </li> In InfoPath 2003, click Microsoft SQL Server, and then click Next.

In InfoPath 2007, click Database (Microsoft SQL Server or Microsoft Office Access only), and then click Next.</li> Provide your server name and your logon credentials, and then click Next.</li> Choose the Pubs database, choose the titleauthor table, and then click Next.</li> Click Finish to complete the Data Connection Wizard.

You are returned to the Select Data Source dialog box.</li> In the Select Data Source dialog box, click Open.

You are returned to the Data Source Setup Wizard.</li> Click Next.</li> Select Design data view first, and then click Finish to complete the Data Source Setup Wizard.</li></ol>

Design the query for your form
<ol> In the Data Source task pane, right-click myFields, and then click Add.

The Add Field or Group dialog box appears.</li> Name the new field myPercentage, and then click OK.</li> Move the myPercentage field to your form.</li> In the Controls task pane, double-click the Button control to insert a button.</li> In the Layout task pane, double-click Two-Column Table to add a table to your form.</li> <li>In the Data Source task pane, click dataFields, and then click d:titleauthor.</li> <li>Move :au_id to the first column of your table. Move :title_id to the second column of your table.</li> <li>Right-click the button on your form, and then click Button Properties.

The Button Properties dialog box appears.</li> <li>In the Label box, type Query, and then click Apply.</li> <li>In InfoPath 2003, click Microsoft Script Editor.

In InfoPath 2007, click Edit Form Code.

Microsoft Script Editor opens.</li> <li> Add the following code to the OnClick event handler for your query button: //Retrieve the value of the field that you want to use as a parameter //to the stored procedure. var percentageValue = XDocument.DOM.selectSingleNode(&quot;/dfs:myFields/my:myPercentage&quot;).text; //Set the Command for the Query Adapter of the Data Source. Incorporate the //parameter values that you want to use. XDocument.QueryAdapter.Command = 'execute &quot;dbo&quot;.&quot;byroyalty&quot; ' + percentageValue; //Query the Data Source. XDocument.Query; </li> <li>Save your form template.</li></ol>

Test the query function for the form

 * 1) In InfoPath 2003, click Preview Form.

In InfoPath 2007, click Preview, and then click Form.
 * 1) Type 100 in the My Percentage box, and then click the query button.

The records that match the royalty percentage of 100% appear.
 * 1) Repeat the previous step with other percentage amounts.

For example, you can try 25, 50, and 75 to find the records that match the royalty percentage for 25%, 50%, and 75%.

Create a new form from a Data Source
Follow the steps in the Design a form that uses a store procedure to query data section to create a new form.

Create the stored procedure
<ol> <li>Start SQL Query Analyzer.</li> <li>Provide your server name and your logon credentials, and then click OK.</li> <li> Execute the following SQL statement: use pubs GO sp_helpconstraint titleauthor GO The constraints information for table titleauthor appears. </li> <li> Execute the following SQL statement: ALTER TABLE titleauthor DROP CONSTRAINT ForeignKeyConstraints

Note  is the constraint for FOREIGN KEY constraint definition that you obtain in step3. </li> <li> Create the addnewauthor stored procedure by using the following SQL statement: create PROCEDURE addnewauthor @id varchar(11),@title varchar(6),@order tinyint,@royaltyper int AS SET IMPLICIT_TRANSACTIONS off INSERT INTO titleauthor values (@id,@title,@order,@royaltyper) </li></ol>

Design the Submit function for your form
<ol> <li>In the Controls task pane, double-click the Button control to insert a button for executing the stored procedure.</li> <li>In the Data Source task pane, right-click d:titleauthor, and then click More.</li> <li>In the Select a Control box, click Controls in Layout Table, and then click OK.</li> <li>Right-click the button on your form, and then click Button Properties.

The Button Properties dialog box appears.</li> <li>In the Label box, type Submit, and then click Apply.</li> <li>In InfoPath 2003, click Microsoft Script Editor.

In InfoPath 2007, click Edit Form Code.

Microsoft Script Editor opens.</li> <li> Add the following code to the OnClick event handler for your submit button: //Retrieve the value of the field that you want to use as a parameter //to the stored procedure. var id = XDocument.DOM.selectSingleNode(&quot;/dfs:myFields/dfs:dataFields/d:titleauthor/@au_id&quot;).text; var title = XDocument.DOM.selectSingleNode(&quot;/dfs:myFields/dfs:dataFields/d:titleauthor/@title_id&quot;).text; var order = XDocument.DOM.selectSingleNode(&quot;/dfs:myFields/dfs:dataFields/d:titleauthor/@au_ord&quot;).text; var royaltyper = XDocument.DOM.selectSingleNode(&quot;/dfs:myFields/dfs:dataFields/d:titleauthor/@royaltyper&quot;).text;

//Set the Command for the Query Adapter of the Data Source. Incorporate the //parameter values that you want to use. XDocument.QueryAdapter.Command = &quot;execute addnewauthor &quot; + &quot;'&quot; + id + &quot;',&quot;+&quot;'&quot;+title+&quot;',&quot;+&quot;'&quot;+order+&quot;',&quot;+&quot;'&quot;+royaltyper+&quot;'&quot;; //Query the Data Source. XDocument.Query; </li> <li>Save your form template.</li></ol>

Test the Submit function for the form
<ol> <li>Type 172-32-1172 in the Au Id box, type BU1031 in the Title Id box, type 2 in the Au Ord box, and type 120 in the Royaltyper box.</li> <li>Click the submit button.</li> <li> In SQL Query Analyzer, execute the following SQL statement: select * from titleauthor where au_id = '172-32-1172' The data is inserted into the titleauthor table. </li></ol>

Additional query words: IP2007

Keywords: kbhowto KB827007

-

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

© Microsoft Corporation. All rights reserved.