Lesson 25: Creating Database Using ADO Control

Learn to create powerful database applications with ADO Control for flexible and cross-platform database solutions

Key Takeaway

ADO (ActiveX Data Objects) provides a more flexible and powerful approach to database programming in VB6, allowing access to various data sources beyond traditional databases.

Welcome to Lesson 25 of our Visual Basic 6 Tutorial! In this lesson, you'll learn how to create database applications using ADO Control, which offers more flexibility than the standard Data Control. ADO works with various data sources and can be used across different platforms and programming languages.

25.1 Introduction to ADO Control

ADO stands for ActiveX Data Objects. Unlike the standard Data Control, ADO can work with many different kinds of data including:

Web Data

Data displayed in Internet browsers

Email Text

Access and manage email content

Graphics

Work with graphical data beyond traditional databases

25.1.1 Adding ADO Control to Toolbox

To use ADO data control, you need to insert it into the toolbox:

1Open Components Dialog

Press Ctrl+T to open the Components dialog box

2Select ADO Control

Check Microsoft ActiveX Data Control 6

3Confirm

Click OK to add ADO control to your toolbox

Adding ADO Control to Toolbox
Figure 25.1: Adding ADO Control to the Toolbox

25.2 Building a Library Application

We'll create a library application to demonstrate ADO capabilities. First, name the new form as frmBookTitle and change its caption to Book Titles - ADO Application.

Library Application Interface
Figure 25.2: The Runtime Interface

25.2.1 Property Settings

Here are the property settings for our library application:

Table 25.1: Property Settings
Control Property Setting
Form Name frmBookTitle
Form Caption Book Titles - ADO Application
ADO Name adoBooks
Label1 Name lblApp
Label1 Caption Book Titles
Label 2 Name lblTitle
Label2 Caption Title :
Label3 Name lblYear
Label3 Caption Year Published:
Label4 Name lblISBN
Label4 Caption ISBN:
Label5 Name lblPubID
Label5 Caption Publisher's ID:
Label6 Name lblSubject
Label6 Caption Subject :
TextBox1 Name txtitle
TextBox1 DataField Title
TextBox1 DataSource adoBooks
TextBox2 Name txtPub
TextBox2 DataField Year Published
TextBox2 DataSource adoBooks
TextBox3 Name txtISBN
TextBox3 DataField ISBN
TextBox3 DataSource adoBooks
TextBox4 Name txtPubID
TextBox4 DataField PubID
TextBox4 DataSource adoBooks
TextBox5 Name txtSubject
TextBox5 DataField Subject
TextBox5 DataSource adoBooks
Command Button1 Name cmdSave
Command Button1 Caption &Save
Command Button2 Name cmdAdd
Command Button2 Caption &Add
Command Button3 Name cmdDelete
Command Button3 Caption &Delete
Command Button4 Name cmdCancel
Command Button4 Caption &Cancel
Command Button5 Name cmdPrev
Command Button5 Caption &<
Command Button6 Name cmdNext
Command Button6 Caption &>
Command Button7 Name cmdExit
Command Button7 Caption E&xit

25.3 Connecting to Database

To connect ADO to a database file (we'll use BIBLIO.MDB that comes with VB6):

1Open Properties

Click on the ADO control and open properties window

2Set ConnectionString

Click on ConnectionString property to open Property Pages

3Select Provider

Choose "Use Connection String" then click Build

Property Pages Dialog
Figure 25.3: Property Pages Dialog
Data Link Properties
Figure 25.4: Data Link Properties

At the Data Link dialog box, double-click the option labeled Microsoft Jet 3.51 OLE DB provider. Then click Next to select the file BIBLO.MDB. Finally, click on the RecordSource property and set the command type to adCmd Table and Table name to Titles.

RecordSource Settings
Figure 25.5: RecordSource Settings

25.4 Writing Code for Command Buttons

After setting up the interface and database connection, we need to write code for the command buttons:

25.4.1 Save Button

SaveButton.frm
Private Sub cmdSave_Click()
    adoBooks.Recordset.Fields("Title") = txtTitle.Text
    adoBooks.Recordset.Fields("Year Published") = txtPub.Text
    adoBooks.Recordset.Fields("ISBN") = txtISBN.Text
    adoBooks.Recordset.Fields("PubID") = txtPubID.Text
    adoBooks.Recordset.Fields("Subject") = txtSubject.Text
    adoBooks.Recordset.Update
End Sub
                        

25.4.2 Add Button

AddButton.frm
Private Sub cmdAdd_Click()
    adoBooks.Recordset.AddNew
End Sub
                        

25.4.3 Delete Button

DeleteButton.frm
Private Sub cmdDelete_Click()
    Dim Confirm As Integer
    Confirm = MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deletion Confirmation")
    
    If Confirm = vbYes Then
        adoBooks.Recordset.Delete
        MsgBox "Record Deleted!", , "Message"
    Else
        MsgBox "Record Not Deleted!", , "Message"
    End If
End Sub
                        

25.4.4 Cancel Button

CancelButton.frm
Private Sub cmdCancel_Click()
    txtTitle.Text = ""
    txtPub.Text = ""
    txtPubID.Text = ""
    txtISBN.Text = ""
    txtSubject.Text = ""
End Sub
                        

25.4.5 Previous Button

PreviousButton.frm
Private Sub cmdPrev_Click()
    If Not adoBooks.Recordset.BOF Then
        adoBooks.Recordset.MovePrevious
        If adoBooks.Recordset.BOF Then
            adoBooks.Recordset.MoveNext
        End If
    End If
End Sub
                        

25.4.6 Next Button

NextButton.frm
Private Sub cmdNext_Click()
    If Not adoBooks.Recordset.EOF Then
        adoBooks.Recordset.MoveNext
        If adoBooks.Recordset.EOF Then
            adoBooks.Recordset.MovePrevious
        End If
    End If
End Sub
                        

Lesson Summary

In this lesson, you've learned how to create database applications using ADO Control in VB6:

ADO Advantages

More flexible than Data Control, works with various data sources

Setup Process

Adding ADO control to toolbox and connecting to databases

Application Design

Building a complete library application with record manipulation

Database Connection

Configuring ConnectionString and RecordSource properties

Important Note

ADO provides a more modern approach to database programming in VB6 and supports connection to various data sources beyond traditional databases, including web data and email content.

Practice Exercises

Enhance your ADO database application with these exercises:

Exercise 1: Connection Testing

Add a "Test Connection" button to verify the database connection before proceeding.

Exercise 2: Record Count

Display the total number of records in a status bar or label.

Exercise 3: Search Function

Implement a search feature to find books by title or ISBN.

Exercise 4: Data Validation

Add validation to ensure required fields are filled before saving.

Exercise 5: Error Handling

Implement proper error handling for database operations.

Next Lesson

Continue your VB6 journey with Lesson 26: DataGrid Control.

Related Resources

Database Basics

Review database fundamentals from Lesson 23

View Lesson

Advanced Database

Previous lesson on advanced database techniques

Previous Lesson

DataGrid Control

Learn to display multiple records with DataGrid

Next Lesson

ADO Sample Code

Practical VB6 ADO code samples

View Examples