36
Visual Basic 6 Black Book:Databases: Using DAO, RDO, And ADO
Like most controls, the DAO, RDO, and ADO controls have methods, events, and
properties. To make these controls consistent, Microsoft has given them the same core
methods, and we’ll take a look at those methods in this chapter. Using these methods,
users can add records to a database, change those records, delete them, and move
around in the database.
In the next few topics, we’ll develop the program you see in Figure 24.18, where we’re
editing the db.mdb database we developed in the beginning of the chapter. Because all
data controls have the same core methods, we’ll use a data control, Data1, in this
example to keep this example easy. We also use two text boxes, Text1 and Text2,
connected to Data1 and the Name and Grade fields in our database, respectively. Now
all we have to do is to make all the buttons in the program active, and we’ll do that in
the following few topics. The code for this example is located in the dbmethods folder
on this book’s accompanying CD-ROM.
Figure 24.18 Using data control methods to add, edit, and delete records.
Adding Records To Databases
You can add a new record to a database with the AddNew method of the Recordset
property of a data or ADO data control, or of the Resultset property of a remote data
control. Let’s see an example. When the user clicks the Add button in the dbmethods
example we’re developing in this and the previous few topics, we can add a new record
like this:
Private Sub cmdAdd_Click()
Data1.Recordset.AddNew
End Sub
This adds a new, blank record. You can enter the data you want in the record’s fields,
and to update the database, you click the Update button.
Deleting Records In Databases
You can delete a record in a database with the Delete method of the Recordset
property of a data or an ADO data control, or of the Resultset property of a remote
data control. Let’s see an example. When the user clicks the Delete button in the
dbmethods example we’re developing in this and the previous few topics, we can
delete a record like this:
Private Sub cmdDelete_Click()
Data1.Recordset.Delete
...
file:///E|/Program%20Files/KaZaA/My%20Shared%...Basic%20-%20%20Black%20Book/ch24/841-845.html (3 of 4) [7/31/2001 9:04:32 AM]