Dear John, How Do I... Create a Control for Use with a Database?

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:

  1. Load the control's project in Visual Basic.

  2. From the Tools menu, choose Procedure Attributes to display the Procedure Attributes dialog box.

  3. Click on the Advanced button. Visual Basic displays the full Procedure Attributes dialog box, as shown in Figure 6-9.

  4. In the Name combo box, select the name of the property to bind.

  5. Click on the Property Is Data Bound check box. Select the other check boxes in the Data Binding group as described below and click OK.

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:

  1. Start a new Standard EXE project.

  2. Add the DatCtl.VBP project to create a new project group.

  3. Draw a Data control on a form, and set the control's DatabaseName property to the NWIND.MDB database installed in the Visual Basic program directory. Set the RecordSource property to Employees.

  4. Draw the Employee control on the form, and click the ellipsis button in the DataBindings property. Visual Basic displays the Data Bindings dialog box as shown in Figure 6-13.

Figure 6-13. Use the Data Bindings dialog box to associate properties with specific fields in a recordset.

  1. Select the FirstName property in the Property Name list and select Data1 from the Data Source drop-down list. Select FirstName from the Data Field drop-down list.

  2. Repeat step 5 for each of the properties in the Employee control, and then click OK.

  3. Run the project. As you use the Data control to move between records, the Employee control displays the information from the NWIND database, as shown in Figure 6-14.

Figure 6-14. Using the Employee control with the Data control to display records from NWIND.MDB.