Saturday, October 6, 2012

Access the Main & Secondary Data Source

Main Data Source
If you would like to write code that access data on an InfoPath form, the first step to access data stored within a form is to get a reference to the main source of the form.

Add the following code to the FormEvents_Loading() event handler

Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator()

Secondary Data Source
If you have a form that has one or more secondary data sources associated with it and you want to access the data from one of those data sources.

In the case below you would like to access a secondary data source called "Employee" for example.

Add the following code to the FormEvents_Loading() event handler

Dim secDS As XPathNavigator = DataSources("Employee").CreateNavigator()

The DataSources property of the Xmlform object is a DataSourceCollection object, which contains DataSources Objects corresponding to each data source of the form with the Main data source of the form listed as the first data source in the colllection. You can access a DataSource object either by its position in the collection or by its name. For example:

Dim mainDS as DataSource = Datasources(0)
or
Dim mainDS as DataSource = DataSources("")
or
Dim mainDS as DataSource = MainDataSource

They all would return a DataSource object for the Main data source of a form.

Dim EmployeeDS as DataSource = DataSources(1)
or
Dim EmployeeDS as DataSource = DataSources("Employee") 

Would return a DataSource object for the Employee secondary data source.

Once you have retrieved an XPathNavigator object, you can use its methods to manipulate the data stored in the data sources.


No comments:

Post a Comment

About Me

My photo
Toronto, Ontario, Canada
MBA, M.Sc. & MCP