LESSON 8 - Visual Basic Database Programming
LESSON 8 - Visual Basic Database Programming
8
ActiveX Data Objects (ADO)
Microsoft’s latest set of data access objects are the ActiveX Data Objects (ADO). These
objects let you access data in a database server through any OLE DB provider. ADO is intended
to give you a consistent interface for working with a wide variety of data sources, from text files to
ODBC relational databases to complex groups of databases.
The way Microsoft implements connections to all those data sources is with the OLE DB set of
COM interfaces, but that standard is a very complex one. Our interface to that interface, so to
speak, is ADO, a set of objects with properties, events, and methods. Here are the ADOs:
There are three data-bound controls that are specially optimized for use with the ADO data
control:
• DataGrid controls
• DataCombo controls
• DataList controls
Don’t confuse these controls with the non-ADO optimized data-bound controls like the DBCombo
and DBList controls. These controls are specifically designed to work with ADO data controls and
won’t work with standard controls like the data control.
Here are the principal data properties you use with these three controls:
• DataGrid - DataSource = ADO data control’s name. You can also set the
AllowAddNew, AllowDelete, AllowUpdate properties to True or False to enable
or disable those operations.
• DataCombo - DataSource = ADO data control’s name; DataField = Name of the
field to display in the combo’s text box; ListField = Name of field to display in the list;
RowSource = ADO data control’s name; and BoundColumn = Name of the source field
with which you can provide data to another control.
• DataList - DataSource = ADO data control’s name; DataField = Name of the field to
display in the current selection, ListField = Name of field to display in the list,
RowSource = ADO data control’s name, BoundColumn = Name of the source field with
which you can provide data to another control.
ActiveX Data Objects (ADO) access data from OLE DB providers. The Connection object is
used to specify a particular provider and any parameters. To connect to a data source, you use a
Connection object. Using that connection, you can create a new record set, and using the
Recordset object’s methods and properties, you can work with your data.
An ADO transaction marks the beginning and end of a series of data operations that are executed
across a connection. ADO makes sure that changes to a data source resulting from operations in
a transaction either all occur successfully, or not at all. If you cancel the transaction or one of its
operations fails, then the result will be as if none of the operations in the transaction had
occurred.
Establishing a Connection
The first step in editing an ADO database is to open that database, which is called a data source
in ADO terminology, by setting up a Connection object. To use that and other ADO objects in
code, you use the Project >> References item, select the Microsoft ActiveX Data Objects
Library item, and click on OK, adding the ADO Object Library to your program.
A Connection object represents a physical connection to a data source. The Connection object
maintains information about the data provider. To create a Connection object, you supply the
name of either an ODBC data source or an OLE DB provider. To support as many data sources
as possible, you can use ADO and ODBC to access a database.
Now we’re free to create a new ADO Connection object with the Connection object’s Open
method:
End Sub
Now we have a connection to the data source. To actually work with the data in that data source,
we’ll create an ADO recordset.
Once you finish with the connection, you use the Close method to disconnect from a data
source. It is proper coding technique to close all open connections before the application is
terminated.
The following code closes an active connection to a data source and releases the connection
object variable:
Information and Communication Technology Department 66
Palompon Institute of Technology
VISUAL BASIC DATABASE PROGRAMMING
8
Now that we have created an ADO connection, we can open a recordset from that connection
using the Recordset object’s Open method:
End Sub
Now that we’ve open our Recordset, we can bind that Recordset to various controls, like
textboxes, etc.
Once a Recordset has been created, you can access the fields of each record in one of the
following ways:
• Refer to the name of the field. If you know the name of the field you want to access,
you can use the following syntax to access the current value of the field.
RecordsetObject!FieldName
For example:
rsStudent!LName = “Smith”
rsStudent!FName = “John”
Information and Communication Technology Department 69
Palompon Institute of Technology
VISUAL BASIC DATABASE PROGRAMMING
8
• Use the field’s collection. You can use the Recordset object’s Fields collection. This
technique is not as efficient when accessing a Recordset. Use the following syntax to
access the value of a field using the Fields collection:
For example:
rsStudent.Fields(“LName”) = “Smith”
rsStudent.Fields(“FName”) = “John”
To bind a control to an ADO Recordset object, you just set that control’s DataSource property to
that object, and then set whatever other data properties those control needs to have set.
End Sub
We can also bind more complex controls to a recordset. The following illustrates how to bind
complex controls to a recordset.
‘Fill the DataGrid control with data from the Student table
Set dtaGridStudent.DataSource = rsStudent
To add a new record to an ADO record set, we use the AddNew method. After you’ve updated the
fields of the current record, you save that record to the database with the Update method.
The following example uses the AddNew method to add a new record:
rsStudent.AddNew
rsStudent!LastName = txtLName.text
rsStudent!FirstName = txtFName.text
rsStudent.Update
After changing the data in a record’s fields or adding a new record, we update the data source to
record the changes, using the Update method:
recordset.Update
For example, to update record in a Recordset (using data supplied by the user in the textbox):
rsStudent!LName = txtLastName.text
rsStudent!FName = txtFirstName.text
rsStudent.Update
End Sub
The following example deletes the current record in the rsStudent recordset:
rsStudent.Delete
End Sub
ADO provides methods and properties to navigate and monitor recordsets. A recordset’s
navigation options may differ, depending on the cursor type and location. You need to consider
what navigation requirements are needed before opening a Recordset.
Of all the ADO objects, only the Recordset object allows users to navigate through a set of
records. Only one record within a Recordset is current at a given time, therefore, the Recordset
object supports a number of properties and methods that allow users to navigate through the
Recordset.
The following lists the properties of the Recordset object that are used to navigate a Recordset.
Property Description
AbsolutePage Sets or returns the database page in which the current record exists.
AbsolutePosition Sets or returns the absolute position of the current record (this can be
affected by record additions or deletions).
BOF Indicates if the record pointer has moved before the first record.
Bookmark Returns a unique identifier for the current record. Setting this property to
a specific record’s bookmark moves the record pointer to that record.
EOF Indicates if the record pointer has moved past the last record.
If rsStudent.EOF then
End if
To move to the first record in an ADO record set, we use the Recordset object’s MoveFirst
method. For example:
Information and Communication Technology Department 73
Palompon Institute of Technology
VISUAL BASIC DATABASE PROGRAMMING
8
rsStudent.MoveFirst
End Sub
To move to the last record in an ADO record set, we use the Recordset object’s MoveLast
method. For example:
rsStudent.MoveLast
End Sub
To move to the next record in an ADO recordset, we use the Recordset object’s MoveNext
method. To make sure that we don’t move past the end of the recordset we use the recordset’s
EOF property:
End Sub
To move to the previous record in an ADO recordset, you use the Recordset object’s
MovePrevious method. To make sure that we don’t move past the beginning of the recordset
we use the recordset’s BOF property
Once a connection has been established, you can begin any transaction to the database. By
using the Connection object’s Execute method, you can send SQL commands to the database
without having to return records to the client.
conStudentDB.BeginTrans
conStudentDB.CommitTrans
conStudentDB.Close
End Sub
We can also insert a record to a Recordset using the values supplied by the user in a textbox or
any other control. For example, we have two textboxes named txtLastName and
txtFirstName, we can insert record to a Recordset using the following code:
OR
If you are using the data supplied by the user in the txtLastName and txtFirstName
textboxes.
OR
If you want to use the data supplied in the txtLastName or txtFirstName textbox.
RecordsetObject.Requery