Microsoft Office 2000 Developer   

Binding any Control to a Data Source Using the BindingCollection Object

See Also

To bind a data source to a non data-aware control that does not have a DataSource property, you need to use the Microsoft Data Binding Collection. The Data Binding Collection permits any control to be bound to the Data Environment, the ADO Data Control, or ADO Recordset objects.

Adding Binding Objects to the Collection

The BindingCollection object is a collection that has a DataSource property. After setting the DataSource property to a data source, you add Binding objects to the collection. Each Binding object binds a field from the data source to an appropriate property of any control. For example, to bind an Access TextBox control to a data source, the following code could be used:

BmCustomers.Add TextBox1, "Text", "CompanyName"

To use the BindingCollection object

  1. On the Tools menu, click References.

  2. In the References dialog box, select the Microsoft Data Binding Collection. Click OK.

  3. Configure a data source, such as the ADO Recordset object, to access data. For more information see Creating a Data Source Using the ADO Data Control.

  4. Create an instance of an object variable of type BindingCollection.

  5. Set the DataSource property (TextBox) of the object to the data source (ADODC).

  6. Using the Add method, add a new member to the collection.

Example Code

Option Explicit
Private bmCustomers As New BindingCollection
' Assuming an ADO Data Control configured to connect to the Customers
' table. The TextBox is named Text1; the code binds the CompanyName
' field to the Text property.

Private Sub UserForm_Initialize()
   Set bmCustomers.DataSource = ADODC1
   BmCustomers.Add TextBox1, "Text", "CompanyName"
End Sub

For More Information   For an example of binding controls using the BindingCollection object, see Adding Multiple Data Sources to an Access Form. For more information about the Data Binding Collection, see "Binding a Control to a Data Source" in the Visual Basic Programmer's Guide.