You are on page 1of 6

Database connectivity using ADO (ActiveX data Object)

To be able to access and manage a database, you need to connect the ADO data control to
a database file. We are going to use BIBLIO.MDB that comes with VB6. To connect ADO to
this database file, follow the steps below:

1.) Open a new VB project and from the Components window (by clicking Project -> Components
menu), select Microsoft Activex Data Control 6.0 (OLEDB) and click OK. ADO data control will
appear in the toolbox.

2.) From toolbox, Drag & Drop Activex Data object data control (ADODC1) on your form.

3.) Click on the ADO control on the form and it will open the properties window.

4.) Click on the General tab and check the ConnectionString property

When the dialog box appears, select the Use Connection String's Option. Next, click build command
button and at the Data Link Properties window, double-Click the option Microsoft Jet 3.51 OLE DB
provider.
 After that, click the Next button to select the database name BIBLO.MDB. You can click on
Test Connection button to ensure proper connection of the database file. A messagebox will
appear, Click OK to finish the connection.
5.) Finally, click on the RecordSource property and set the command type to adCmd
Table and Table name to Customers. Now you are ready to use the database file.
6.) Drag & Drop a Textbox control (Text1) on your form, and set the properties as follows: 

Property Value
DataSourceName Select ADO data control name: ADODC1
DataField Select field of table to be displayed: CustomerID

7.) Now repeat the above step for all other text boxes on the form to set their DataSource, and
DataField properties.

8.) Run the form by pressing F5 key.


The program codes for add, delete, cancel, movefirst, movelast, next, previous
operations on click event of command buttons are as follow:

Private Sub cmdSave_Click()

ADODC1.Recordset.Fields("Title") = txtTitle.Text
ADODC1.Recordset.Fields("Year Published") = txtPub.Text
ADODC1.Recordset.Fields("ISBN") = txtISBN.Text
ADODC1.Recordset.Fields("PubID") = txtPubID.Text
ADODC1.Recordset.Fields("Subject") = txtSubject.Text
ADODC1.Recordset.Update

End Sub

======================

Private Sub cmdAdd_Click()

ADODC1.Recordset.AddNew

End Sub

=====================

Private Sub cmdDelete_Click()

ADODC1.Recordset.Delete
End Sub

====================================== 

Private Sub cmdCancel_Click()

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""

End Sub

For the Previous (<) button, the program codes are


Private Sub cmdPrev_Click()

ADODC1.Recordset.MovePrevious

End Sub
===================
For the Next(>) button, the program codes are

Private Sub cmdNext_Click()

ADODC1.Recordset.MoveNext

End Sub

=================

Private Sub cmdFirst_Click()

ADODC1.Recordset.MoveFirst

End Sub

================

Private Sub cmdLast_Click()

ADODC1.Recordset.MoveLast

End Sub

You might also like