Visual Basic Concepts
The DataRepeater control functions as a data-bound container of any user control you create. For example, imagine creating a user control that contains three TextBox controls, and a CheckBox control. This user control is designed to show one record of an employee database — displaying name, birth date, employee number, and marital status of the employee.
After compiling the control into an .ocx, the DataRepeater control's RepeatedControlName property is set to the user control. The DataRepeater is then bound to a data source, such as the ADO Data Control, which sets up a connection between the user control and the employee database. At run time, the DataRepeater displays several instances of the user control—each in its own row, and each bound to a different record in the database. The result can resemble the figure below.
Employee Records Repeated in DataRepeater Control
At run time, the user can scroll through a recordset using the HOME, END, PAGEUP, PAGEDOWN, and arrow keys.
Possible Uses
The first step when using the DataRepeater control is to create a data-bound user control. The procedure below creates a simple control that can be repeated in the DataRepeater.
For More Information Details about creating a data-bound user control can be found in "Binding a Control to a Data Source."
Creating a data-bound User control for use in the DataRepeater Control
Object | Property | Setting |
Text1 | Name | txtProductName |
Text2 | Name | txtUnitPrice |
Label1 | Caption | Product Name |
Label2 | Caption | Unit Price |
Because the user control will be repeated, you may want to minimize its height; the simple user control described above looks like this:
Public Property Get ProductName() As String
ProductName = txtProductName.Text
End Property
Public Property Let ProductName(ByVal newProductName As String)
txtProductName.Text = newProductName
End Property
Public Property Get UnitPrice() As String
UnitPrice = txtUnitPrice.Text ' Return a String!
End Property
Public Property Let UnitPrice(ByVal newUnitPrice As String)
txtUnitPrice.Text = newUnitPrice ' NewUnitPrice is a String!
End Property
Private Sub txtProductName_Change()
PropertyChanged "ProductName"
End Sub
Private Sub txtUnitPrice_Change()
PropertyChanged "UnitPrice"
End Sub
Important Notice in the above code that the UnitPrice property is declared as a string. This is to allow the user of the DataRepeater control to use the DataFormat object to format the string as Currency. If you type the new value as Currency (what you might expect), the formatting supplied by the DataFormat object will be stripped.
On the Tools menu click Procedure Attributes. On the Procedures Attributes dialog box, click Advanced. The Name box contains the property you want to make data-bound, and should contain ProductName. Click Property is data bound, then click Show in DataBindings collection at design time. Click the Name box and click UnitPrice. Once again, click Property is data bound, then click Show in DataBindings collection at design time. Click OK to close the dialog box.
When you compile the user control into an .ocx, Visual Basic registers the control for you, allowing you to use it in the DataRepeater control.
Once you have built and compiled a data-bound user control, you can have it repeated in the DataRepeater control.
Using a data-bound user control in the DataRepeater control
Object | Property | Setting |
Project1 | Name | prjRepeater |
Form1 | Name | frmRepeater |
On the Project menu, click Components. In the Components dialog box, click the Controls tab, and check Microsoft Data Repeater Control and Microsoft ADO Data Control. Click OK to close the dialog box.
SELECT * FROM Products
Once the user control is contained by the DataRepeater control, you must bind the user control's properties to the record source.
When creating a user control to be used in a DataRepeater control, be aware that public properties of the control will be set only on the current control (the "live" control with the focus). For example, if you expose the Font property of a user control, at run time, resetting that property (as shown in the example code below) will only affect the current control in the DataRepeater control. The font of repeated controls will not be affected.
Private Sub Command1_Click()
' Only the current control's Font will be affected.
DataRepeater1.RepeatedControl.FontName = "Courier"
End Sub
To actually change the font for every control, you must expose the Font object's Name property as a public property of the contained control:
Public Property Get FontName() As String
FontName = txtProductName.Font.Name
End Property
Public Property Let FontName(ByVal newFontName As String)
txtProductName.Font.Name = newFontName
End Property