Dear John, How Do I... Use the DataRepeater Control?
The DataRepeater control is a type of container for the data-bound
ActiveX controls you create. To use the DataRepeater control you must already
have created a compiled, data-bound control such as the Employee control
described in the earlier section, "Dear John, How Do I... Create a Control for Use with a Database?"
The DataRepeater control lets you replace page views of a database
with a scrolling list. It's sort of like the FlexGrid control, but you can include
controls within the grid in addition to data. Figure 6-15 demonstrates the
differences among a form designed for viewing a database table, a FlexGrid view of the
same table, and a view using the DataRepeater control.
Figure 6-15. A custom form, a FlexGrid control, and a DataRepeater control offer three different ways to view employee records in the NWIND database.
NOTE
The DataRepeater control requires a compatible data
source, such as the ADO Data control. The DataRepeater control won't
work with the intrinsic Data control.
To use the DataRepeater control, follow these steps:
- Choose Components from the Project menu. Visual Basic
displays the Components dialog box.
- Select the Microsoft DataRepeater Control 6.0 and the
Microsoft ADO Data Control 6.0 check boxes, and then click OK. Visual
Basic adds the ADO Data control and the DataRepeater control to
the Toolbox.
- Draw a DataRepeater control on a form.
- Draw an ADO Data control on the same form as the
DataRepeater control.
- Click the ellipsis button in the ADO Data control's
ConnectionString property to display the Property Pages dialog box.
- Select the Use ODBC Data Source Name option, and then select
a data source from the drop-down list (skip steps 7-11) or create a
new data source by clicking the New button. (Steps 7-11 explain how
to create a new ODBC Microsoft Access data source.)
- Select the System Data Source (Applies To This Machine Only)
option in the Create New Data Source window, and then click Next.
- Select Microsoft Access Driver (*.MDB) in the Name list,
click Next, and then click Finish.
- Type in a name for the data source in the Data Source Name
text box in the ODBC Microsoft Access Setup window.
- Click the Select button, and choose the Access database for this
data source. Click OK to close the ODBC Microsoft Access Setup window.
- Choose the Use ODBC Data Source Name option, select the
data source you created in step 9 from the drop-down list, and
then click OK.
- Click the ellipsis button in the ADO Control's RecordSource
property to display the Property Pages dialog box.
- Select 2 - adCmdTable from the Command Type drop-down
list, select a table from the Table Or Stored Procedure Name
drop-down list, and then click OK.
- Select the DataRepeater control and set its DataSource property
to the name of the ADO Data control created in step 4.
- Set the DataRepeater control's RepeatedControlName property
to the name of a data-bound ActiveX control.
- Click the ellipsis button in the DataRepeater control's
(Custom) property to display the Property Pages dialog box. Click on
the RepeaterBindings tab to display the property page shown in
Figure 6-16.
- Select a property name from the PropertyName drop-down list and
a record field to display in that property from the DataField
drop-down list, and then click Add.
- Repeat step 8 for each of the properties in the data-bound
control. Click OK when done.
Figure 6-16. Bind the repeated control's properties to the fields in a record using the DataRepeater control's Property Pages dialog box.
At runtime, you can scroll through records using the arrows on the
ADO Data control or the scroll bar on the DataRepeater control. For this reason,
you may want to hide the ADO Data control at runtime.