DAO
DAO
DAO
INTRODUCTION TO DAO.
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.
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.
FindFirst: Locates the first record in the Recordset with the given criteria. The search begins at the beginning of the Recordset. Syntax: rs.FindFirst.
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
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).
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
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
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.
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.
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.
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.