The ADO Data control uses Microsoft ActiveX Data Objects (ADO) to quickly create connections between data-bound controls and data providers. Data-bound controls are any controls that feature a DataSource property. Data providers can be any source written to the OLE DB specification. You can also easily create your own data provider using Visual Basic's class module.
Although you can use the ActiveX Data Objects directly in your applications, the ADO Data control has the advantage of being a graphic control (with Back and Forward buttons) and an easy-to-use interface that allows you to create database applications with a minimum of code.
Figure 7.4 The ADO Data Control
Several of the controls found in Visual Basic's Toolbox can be data-bound, including the CheckBox, ComboBox, Image, Label, ListBox, PictureBox, and TextBox controls. Additionally, Visual Basic includes several data-bound ActiveX controls such as the DataGrid, DataCombo, Chart, and DataList controls. You can also create your own data-bound ActiveX controls, or purchase controls from other vendors.
Previous versions of Visual Basic featured the intrinsic Data control and the Remote Data control (RDC) for data access. Both controls are still included with Visual Basic for backward compatibility. However, because of the flexibility of ADO, it's recommended that new database applications be created using the ADO Data Control.
For More Information A complete list of data-bound controls can be found in "Controls That Bind to the ADO Data Control." To find out how to use the intrinsic Data control or the Remote Data control, see "Using the Data Control" or "Using the Remote Data Control." For details about creating a data provider, see "Creating Data-Aware Classes."
To create a client, or front-end database application, add the ADO Data control to your forms just as you would any other Visual Basic control. You can have as many ADO Data controls on your form as you need. Be aware, however, that the control is a comparatively "expensive" method of creating connections, using at least two connections for the first control, and one more for each subsequent control.
It's possible to create a database application using a minimum of code by setting a few properties at design time. If you are using an OLE DB data source, the Microsoft Data Link Name (.UDL) must be created on your machine. See "Creating the Northwind OLE DB Data Link" for a step-by-step example.
To create a simple front-end database application
If the control is not available in the Toolbox, press CTRL+T to display the Components dialog box. In the Components dialog, click Microsoft ADO Data Control.
driver={SQL Server};server=bigsmile;uid=sa;pwd=pwd;database=pubs
SELECT * FROM Titles WHERE AuthorID = 72
You should always include a WHERE clause when accessing a table. Failing to do so will lock the entire table, which would be a major hindrance to other users.
The code below shows how to set these four properties programmatically. Note that setting the DataSource property requires the Set statement.
Private Sub Form_Load()
With ADODC1
.ConnectionString = "driver={SQL Server};" & _
"server=bigsmile;uid=sa;pwd=pwd;database=pubs"
.RecordSource = "Select * From Titles Where AuthorID = 7"
End With
Set Text1.DataSource = ADODC1
Text1.DataField = "Title"
End Sub
The ADO Data control features several events that you can program. The table below shows the events and when they occur; however the table is not meant to be a complete list all of the conditions when the events occur. For complete information, see the reference topic for the individual event.
Event | Occurs |
WillMove | On Recordset.Open, Recordset.MoveNext, Recordset.Move, Recordset.MoveLast, Recordset.MoveFirst, Recordset.MovePrevious, Recordset.Bookmark, Recordset.AddNew, Recordset.Delete, Recordset.Requery, Recordset.Resync |
MoveComplete | After WillMove |
WillChangeField | Before the Value property changes |
FieldChangeComplete | After WillChangeField |
WillChangeRecord | On Recordset.Update, Recordset.Delete, Recordset.CancelUpdate, Recordset.UpdateBatch, Recordset.CancelBatch |
RecordChangeComplete | After WillChangeRecord |
WillChangeRecordset | On Recordset.Requery, Recordset.Resync, Recordset.Close, Recordset.Open, Recordset.Filter |
RecordsetChangeComplete | After WillChangeRecordset |
InfoMessage | When the data provider returns a result |
For More Information To try other step-by-step procedures using the ADO Data control, see "Creating a Simple Database Application with the DataGrid and ADO Data Control," "Creating a Simple DataCombo Application," and "Creating a DataGrid Linked to a DataList Control."