ActiveX controls can display data from database records by providing a data binding to one or more of the control's properties. A data binding is a relationship between the property and a field in a database record or query. For example, the Blinker control created in the preceding sections could have its Interval property bound to a numeric field in a database. To add a binding to a control property, follow these steps:
The Employee control in Figure 6-12 is a simple data-bound control created for use with the NWIND database that ships with Visual Basic.
Figure 6-12. The Employee control is designed to display fields in the NWIND database's Employee recordset.
The Employee control (DatCtl.VBP) is composed of several labels and a text box which display the data from the Employees table in the NWIND database. The control's FirstName, LastName, HireDate, and Notes properties are defined by the following code:
`Properties section
Public Property Get FirstName()
FirstName = lblFirstName.Caption
End Property
Public Property Let FirstName(Setting)
lblFirstName.Caption = Setting
PropertyChanged FirstName
End Property
Public Property Get LastName() As String
LastName = lblLastName.Caption
End Property
Public Property Let LastName(Setting As String)
lblLastName.Caption = Setting
PropertyChanged LastName
End Property
Public Property Get HireDate() As String
HireDate = lblHireDate.Caption
End Property
Public Property Let HireDate(Setting As String)
lblHireDate.Caption = Setting
PropertyChanged HireDate
End Property
Public Property Get Notes() As String
Notes = txtNotes.Text
End Property
Public Property Let Notes(Setting As String)
txtNotes.Text = Setting
PropertyChanged Notes
End Property
Notice that each of the Property Let procedures includes a PropertyChanged statement. According to the Visual Basic documentation, you should include these statements for all properties that can be data-bound, even if they are available only at runtime.
To use the Employee control, follow these steps:
Figure 6-13. Use the Data Bindings dialog box to associate properties with specific fields in a recordset.
Figure 6-14. Using the Employee control with the Data control to display records from NWIND.MDB.