You are on page 1of 42

Database Connectivity

INTRODUCTION TO DAO.

Data Access Object (DAO) model


The Data Access Object (DAO) model is the

Jet Database Engines Object Oriented interface. It is a hierarchy of classes that correspond to a logical view of a relational system, such as the database itself, the tables defined in it and their fields, indexes and so on. To add this, Select References from the Project menu. Select the DAO 3.51 Object library Click ok.

Opening an existing Database


The OpenDatabase method is used to open a specified database. This method returns a reference to the Database object that represents it.

Syntax: Dim db as Database. Set db = OpenDatabase(.Employee.mdb.);

Db is a variable that represents the Database Object. To open the database for exclusive use, syntax is
Set db = OpenDatabase(.Employee.mdb., True).

The value TRUE indicates that no other user can open the database. The default is False.

DAO
To open the database in the read only mode, syntax is used: Set db = OpenDatabase(.Employee., False, True).

That is the third argument True, will provide only a read access on the database.

Creating a Recordset Object A RecordSet Object is created on a database object. The Recordsets collection of the database object contains all open RecordSet Objects created. The RecordSet object also has Fields that contains all the Field Objects of a record in the RecordSet.

DAO
Opening a RecordSet The OpenRecordSet method is used to create a new RecordSet Object and append it to the RecordSet collection of the Database. Syntax: Dim rs as Recordset Set rs = db.OpenRecordset(.Employee., dbOpenTable, dbReadOnly)

DAO
The following example moves to the fourth record in the Recordset. MoveFirst: The MoveFirst method moves to the first record in the Recordset. Syntax: rs.MoveFirst.

MoveLast: The MoveLast method moves to the last record in the Recordset. Syntax: rs.MoveLast.

MoveNext:

The MoveNext method moves to the next record in the Recordset. The EOF property should be checked to prevent an error occurring. EOF is set to True if the current record is after the last record in the Recordset.

Syntax rs.MoveNext If rs.EOF Then rs.MovePrevious MsgBox "At last record", vbInformation, Database" End If.

MovePrevious:

The MovePrevious method moves to the previous record in the Recordset. The BOF property should be checked to prevent an error occurring. BOF is set to True if the current record is before the first record in the Recordset.

rs.MovePrevious If rs.BOF Then rs.Recordset.MoveNext MsgBox "At first record", vbInformation, "Database" End If.

Locating a Record in a Recordset


The Recordset object has a set of Find methods that enable you to locate a specific record according to some search criteria.

FindFirst: Locates the first record in the Recordset with the given criteria. The search begins at the beginning of the Recordset. Syntax: rs.FindFirst.

Locating a Record in a Recordset


FindLast: Locates the last record in the Recordset with the given criteria. The search begins at the end of the Recordset. rs.FindLast. FindNext: Locates the next record in the Recordset with the given criteria. The search begins at the currentrecord. Syntax: rs.FindNext.

Locating a Record in a Recordset


FindPrevious: Locates the previous record in the Recordset with the given criteria. The search begins at the current record. Syntax: rs.FindPrevious. Adding Records: The AddNew method adds a new record to the end of the Recordset. Syntax: rs.AddNew.

Locating a Record in a Recordset

code
Declaration Dim db As Database. Dim rs As Recordset In the following code, the database is opened in the read only mode and also the table is opened. Set db = OpenDatabase("c:\Employee.mdb", False) Set rs = db.OpenRecordset("Emp", dbOpenTable) End Sub

code
The user has to click on the save button after making changes or inserting new record. Private Sub AddCmd_Click() EmpnoTxt.Text = " " NameTxt.Text = " " DnoTxt.Text = " " rs.AddNew EmpnoTxt.SetFocus End Sub Private Sub ClearCmd_Click() EmpnoTxt.Text = " " NameTxt.Text = " " DnoTxt.Text = " " End Sub

code
Private Sub DelCmd_Click() rs.Delete rs.MoveNext If rs.EOF Then rs.MoveLast End If End Sub

code
Private Sub EditCmd_Click() rs.Edit End Sub Private Sub ExitCmd_Click() End End Sub

code
Private Sub FirstCmd_Click() rs.MoveFirst EmpnoTxt.Text = rs("Empno") NameTxt.Text = rs("Name") DnoTxt.Text = rs("Deptno") End Sub Private Sub LastCmd_Click() rs.MoveLast EmpnoTxt.Text = rs("Empno") NameTxt.Text = rs("Name") DnoTxt.Text = rs("Deptno") End Sub

code
Private Sub NextCmd_Click() rs.MoveNext If rs.EOF Then rs.MoveFirst End If EmpnoTxt.Text = rs("Empno") NameTxt.Text = rs("Name") DnoTxt.Text = rs("Deptno") End Sub

code
Private Sub PrevCmd_Click() rs.MovePrevious If rs.BOF Then rs.MoveFirst End If EmpnoTxt.Text = rs("Empno") NameTxt.Text = rs("Name") DnoTxt.Text = rs("Deptno") End Sub

code
Private Sub SaveCmd_Click() If rs.EditMode = dbEditAdd Then rs("Empno") = EmpnoTxt.Text rs("Name") = NameTxt.Text rs("Deptno") = DnoTxt.Text End If rs.Update End Sub

code
Private Sub UpdateCmd_Click() rs("Empno") = EmpnoTxt.Text rs("Name") = NameTxt.Text rs("Deptno") = DnoTxt.Text rs.Update End Sub

ActiveX Data Objects.


There are two ways for accessing Database Information: ActiveX Data Controls (ADC). ActiveX Data Objects (ADO). ADO uses a new database connection framework called OLE DB, which allows faster access to multiple data providers

Working with ADO Data Control


Start Visual Basics 6.0 and select a new

standard Exe project. Select Project Menu and select Components, you will see a figure as shown below. Select or Check the Microsoft ADO Data Control 6.0 (OLEDB).

Working with ADO Data Control

Working with ADO Data Control

Working with ADO Data Control


Set the ADO Data control.s caption as

Employee Database, and name it as Adodc1. Right click the ADO DC control and select ADO DC properties, the following figure appears.Property pages of ADO DC. Select the

Working with ADO Data Control

Working with ADO Data Control


Select the Use Connection String option, click

on the Build button, the following figure appears. Select Mircrosoft Jet 4.0 OLE DB Provider, as we want to use jet engine to access MsAccess database from the Data Link Properties window that is displayed as shown below. Click on the Next button. Selecting OLEDB provider

Working with ADO Data Control

Working with ADO Data Control


Connection tab is seen selected. Select the

Database required. Click on the Test button, to ensure that the connection is successful. If it is a successful connection you will see a small window indicating that the test is success as shown in figure. Click on the OK button.

Working with ADO Data Control

Working with ADO Data Control


database connection.

Once again click OK button to close the Data Link Properties window. Select RecordSource tab. From the command type list as 1adCmdTable as shown in the figure. Select the table name as Emp or any other required table from the list given. Finally click ok.

Working with ADO Data Control

Working with ADO Data Control


Selecting the respective OLE Db provider in

the Data Link Properties can connect any other Database. Associating the controls with respective properties. Select the Text1 textbox and set the DataSource property as Adodc1. Select the DataField property to the field name Empno as shown in the property sheet below.

Working with ADO Data Control

Working with ADO Data Control


Similarly attach the remaining textboxes

Text2 and Text3 to the Name and Deptno Fields respectively. Save the Project and Form and run the application.You will see the output as shown below. By clicking the navigating keys, you can browse through the records.

Working with ADO Data Control

You might also like