Time Out to Discuss Data Binding
Since we are going to be discussing binding fields of ActiveX controls to ADO recordsets, now is probably a good time to discuss data binding. Data binding is one of the more powerful uses of ActiveX controls. Essentially, using data binding allows a property of the ActiveX control to bind with a specific field in a database.
When the bound control property (such as the text box's
Text property) is modified by the user of our control, the control actually notifies the database that the value has changed. It then requests that the relevant field of the current record be updated. This process takes place, and in return the database notifies the control of the success or failure of the update. Hmmm…sounds like a data control to me.
So in a nutshell, data binding allows a database entry, such as a record field, to be linked to a property of an ActiveX control.
Our ActiveX control is typically hosted in a VB 6.0 form. This form and the bound controls provide the visual interface to the current record. The figure below illustrates a conceptual view of this binding:
In this example, the ActiveX control has two text boxes, each of which has bound its
Text property to the fields of a record. When the user makes changes to either text box, the modifications are made to the control's
Text property. These changes are communicated to the database.
Remember earlier on when we mentioned than an ActiveX control is 'hosted' by a form? An ActiveX control needs a form to live on. Data binding is a notification mechanism that links our ActiveX control's properties - through the container (i.e. our form) to a data source (i.e. a database field).
When we bind an ActiveX control property, we must make sure that the control is able to send notifications to the database whenever the property changes. That notification is sent to the control container (i.e. the hosting VB form), which processes it and returns the database's response to the control.
In order to see the brand-new features that VB 6.0 offers database programmers, we are going to create three projects in this chapter:
- First, we will build a simple form that uses the new VB 6 bindings collection to get a feel for how this is done. We will simply bind two fields in the Publishers table to two text boxes. This will get your mind around how to accomplish this, and it will certainly provide you with food for thought. Next, we will actually create our own ActiveX ADO Bound control. This
.OCXcontrol will manage a recordset for us. You will be able to compile this control and give it to other programmers to use, or you might want to use it in your next project.
- Second, we will use the brand new Data Repeater control that comes with VB 6.0.
- Finally, we will build a data-aware class. This approach gives us the ability to use a class module to bind database fields to controls in a form – without an ADO data control! That's right, with VB 6.0 we can now bind database fields directly to bound controls on a form without the mediation of a data control.
VB 6.0 has really provided database programmers with a wealth of possibilities. Let's take a look.