Use values from a SQL Server database to populate a list box, drop-down list box, or combo box
You can populate a list box, drop-down list box, or combo box with data from a query data connection to a Microsoft SQL Server database.
In this article
Overview
A list box, drop-down list box, or combo box displays items that a user can select when filling out a form that is based on your form template. You can configure these types of controls to show the data from a secondary data connection to a Microsoft SQL Server database. A secondary data connection is any data connection to an external data source that you add to a form template so that users who fill out forms based on this form template can get data from or submit data to an external data source, such as a Web service.
When you add a secondary data connection that queries data to your form template, Microsoft Office InfoPath creates a secondary data source with data fields and groups that corresponds to the way the data is stored in the database. The query data connection gets data from the Web service and stores that data in the secondary data source of the form. By default, the data in the secondary data source is available only while users are connected to a network. You can configure the secondary data connection to save the results of the query on your users' computers so that they have access to the data when their form is not connected to a network.
Security Note: If you are using a secondary data connection to retrieve sensitive data from an external data source, you may want to disable this feature to help protect the data from unauthorized use in case the computer is lost or stolen. If you disable this feature, the data will be available only if the user is connected to the network.
Because the data structure in the secondary data source must match the way that data is stored in the database, you cannot modify existing fields or groups in the secondary data source. Find links to more information about data connections and data sources in the See Also section.
Before you begin
Before you can use values from a SQL Server database to populate a control on your form template, you need the following information from your database administrator:
-
The name of the server with the database that you will use with this form template.
-
The name of the database that you will use with this form template.
-
The authentication required by the database. The database can use either Microsoft Windows authentication or SQL Server authentication to determine how users can access the database.
-
The name of the table that contains the data that you want to send to the form. This is the primary table. If you plan to use more than one table in the database, you need the names of the other, child tables. You also need the names of the fields in the child tables that have relationships to the fields in the primary table.
-
Whether you can safely store the query results on your users' computers for offline use.
Step 1: Add a query data connection
If you do not have an existing query data connection that you can use on your form template, follow these steps to add a secondary data connection to your form template that queries data. If your form template already has a secondary data connection that you can use, you can skip this section and go to Step 2: Configure the control.
-
On the Tools menu, click Data Connections.
-
In the Data Connections dialog box, click Add.
-
In the Data Connection Wizard, click Create a new connection to, click Receive data, and then click Next.
-
On the next page of the wizard, click Database (Microsoft SQL Server or Microsoft Office Access only), and then click Next.
-
On the next page of the wizard, click Select Database.
-
In the Select Data Source dialog box, click New Source.
-
On the next page of the wizard, in the What kind of data source do you want to connect to list, click Microsoft SQL Server and then click Next.
-
In the Server name box, type the name of the server that contains the SQL Server database.
-
Under Log on credentials, do one of the following:
-
If the database determines who has access based on the credentials used in a Microsoft Windows network, click Use Windows Authentication.
-
If the database determines who has access based on a specified user name and password that you get from the database administrator, click Use the following User Name and Password, and then type your user name and password in the User Name and Password boxes.
-
-
Click Next.
-
In the Select the database that contains the data you want list, click the name of the database that you want to use, click Connect to a specific table, click the name of the primary table, and then click Next.
-
On the next page of the wizard, in the File Name box, type a name for the file that stores the data connection information, and then click Finish to save these settings.
If you plan to use other tables in the query data connection, you can add the other tables in this page of the wizard.
Add other tables
-
Click Add Table.
-
In the Add Table or Query dialog box, click the name of the child table, and then click Next. InfoPath attempts to set the relationships by matching field names in both tables. If you do not want to use the suggested relationship, select the relationship, and then click Remove Relationship. To add a relationship, click Add Relationship. In the Add Relationship dialog box, click the name of each related field in the respective column, and then click OK.
-
Click Finish.
-
To add additional child tables, repeat these steps.
-
-
Click Next.
-
To store the results of the query with the form template, select the Store a copy of the data in the form template check box. Selecting this check box stores the query results in the form template. Becuase the data is stored in the form template. the data is available in the forms that users fill out, even if their computers are not connected to a network.
Security Note: Selecting this check box stores the query results in the form template. Because the data is stored in the form template, it is available in the forms that users fill out, even if their computers are not connected to a network. If you are getting sensitive data from this data connection, you may want to disable this feature to help protect the data in case the computer is lost or stolen.
-
Click Next.
-
On the next page of the wizard, type a descriptive name for this secondary data connection. If you want the form to automatically receive data when the form is opened, select the Automatically retrieve data when form is opened check box.
Step 2: Configure the control
-
If the form template has multiple views, click View name on the View menu to go to the view with the control where you want to display the data from the secondary data source.
-
Double-click the list box, drop-down list box, or combo box control that you want to populate.
-
Click the Data tab.
-
Under List box entries, click Look up values from an external data source.
-
In the Data Source list, click the data source that you want to use.
-
Click Select XPath next to the Entries box.
-
In the Select a Field or Group dialog box, specify what data will be shown to the user and what data will be submitted to the external data source by doing one of the following.
Specify that the data the user will see is the same data the user will submit
-
Click a field, and then click OK.
Specify that the data the user will see is different from the data that the user will submit
Note: In this scenario, the data that the user will see is from one of the fields in the group, and the data that the user will submit is in another field in the same group.
-
Click a group, and then click OK.
-
In the Control Properties dialog box, click Select XPath next to the Value box.
-
In the Select a Field or Group dialog box, click a field that contains the data that the user will submit to the external data source, and then click OK.
-
In the Control Properties dialog box, click Select XPath next to the Display name box.
-
In the Select a Field or Group dialog box, click a field that contains the data that will appear in the control, and then click OK.
-
-
If the field in the Display name box has values with similar display names, and you want to display only unique names, select the Show only entries with unique display names check box.
No comments:
Post a Comment