Vous êtes sur la page 1sur 10

Visual Basic

Aim: Develop a small database management system using ADO. Theory: Database Structure In simplest terms, a database is a collection of information. This collection is stored in well-defined tables, or matrices. The rows in a database table are used to describe similar items. The rows are referred to as tabase records. In general, no two rows in a database table will be alike. The columns in a database table provide characteristics of the records. These characteristics are called database fields. Each field contains one specific piece of information. In defining a database field, you specify the data type, assign a length, and describe other attributes.

Practical:10

ADO Data Control: The ADO (ActiveX Data Object) data control is the primary interface between a Visual Basic

application and a database. It can be used without writing any code at all! Or, it can be a central part of a complex database management system. This icon may not appear in your Visual Basic toolbox. If it doesnt, select Project from the main menu, then click Components. The Components window will appear. Select Microsoft ADO Data Control, then click OK. The control will be added to your toolbox. As mentioned in Review and Preview, previous versions of Visual Basic used another data control. That control is still included with Visual Basic 6.0 (for backward compatibility).

Visual Basic
The data control can be used to perform the following tasks: 1. Connect to a database. 2. Open a specified database table. 3. Create a virtual table based on a database query. 4. Pass database fields to other Visual Basic tools, for display or editing. Such tools are bound tools Controls), or data aware. 5. Add new records or update a database. 6. Trap any errors that may occur while accessing data. 7. Close the database.

Data Control Properties: Align: Determines where data control is displayed. Caption: Phrase displayed on the data control. ConnectionString: Contains the information used to establish a connection to a database. LockType: Indicates the type of locks placed on records during editing (default setting makes databases read-only). Recordset: A set of records defined by a data controls ConnectionString and RecordSource properties. Run-time only. RecordSource: Determines the table (or virtual table) the data control is attached to. As a rule, you need one data control for every database table, or virtual table, you need access to. One row of a table is accessible to each data control at any one time. This is referred to as the current record. When a data control is placed on a form, it appears with the assigned caption and four arrow buttons:

The arrows are used to navigate through the table rows (records). As indicated, the buttons can be used to move to the beginning of the table, the end of the table, or from record to record.

Visual Basic
We will be using the BIBLIO.MDB database in our first example, so these steps show you how to create its data link: 1. Open Windows Explorer. 2. Open the folder where you will store your data link file. 3. Right-click the right side of Exp- lorer and choose New. From the list of files, select Microsoft Data Link. 4. Rename the newly created file BIBLIO.UDL 5. Right-click this new UDL file and click Properties. 6. Choose the Provider tab and select Microsoft Jet 3.51 OLE DB Provider (an Access database). 7. Click the Next button to go to the Connection tab. 8. Click the ellipsis and use the Select Access Database dialog box to choose the BIBLIO.MDB file which is in the Visual Basic main folder. Click Open. 9. Click Test Connection. Then, click OK (assuming it passed). The UDL file is now created and can be assigned to ConnectionString using the steps below. If a data link has been created and exists for your database, click the ellipsis that appears next to the ConnectionString property. Choose Use Data Link File. Then, click Browse and find the file. Click Open. The data link is now assigned to the property. Click OK. To make using bound controls easy, follow these steps (in order listed) in placing the controls on a form: 1. Draw the bound control on the same form as the data control to which it will be bound. 2. Set the DataSource property. Click on the drop-down arrow to list the data controls on your form. Choose one. 3. Set the DataField property. Click on the drop-down arrow to list the fields associated with the selected data control records. Make your choice. 4. Set all other properties, as required.

we have learned how to build VB database applications using data control. However, data control is not a very flexible tool as it could only work with limited kinds of data and must work strictly in the Visual Basic environment. To overcome these limitations, we can use a much more powerful data control in Visual Basic, known as ADO control. ADO stands for ActiveX data objects. As ADO is ActiveX-based, it can work in different platforms (different computer systems) and different programming languages. Besides, it can access many different kinds of data such as data displayed in the Internet browsers, email text and even graphics other than the usual relational and non relational database information.

Visual Basic
To be able to use ADO data control, you need to insert it into the toolbox. To do this, simply press Ctrl+T to open the components dialog box and select Microsoft ActiveX Data Control 6. After this, you can proceed to build your ADO-based VB database applications. The following example will illustrate how to build a relatively powerful database application using ADO data control. First of all, name the new Application. form as frmBookTitleand change its caption to Book Titles- ADO Secondly, insert the ADO data control and name it as adoBooks and change its caption

to book. Next, insert the necessary labels, text boxes and command buttons. The runtime interface of this program is shown in the diagram below, it allows adding and deletion as well as updating and browsing of data.

The properties of all the controls are listed as follow: Form Name Form Caption ADO Name Label1 Name Label1 Caption Label 2 Name Label2 Caption Label3 Name Label3 Caption frmBookTitle Book Titles -ADOApplication adoBooks lblApp Book Titles lblTitle Title : lblYear Year Published:

Visual Basic
Label4 Name Label4 Caption Labe5 Name Label5 Caption Label6 Name Label6 Caption TextBox1 Name TextBox1 DataField TextBox1 DataSource TextBox2 Name TextBox2 DataField TextBox2 DataSource TextBox3 Name TextBox3 DataField TextBox3 DataSource TextBox4 Name TextBox4 DataField TextBox4 DataSource TextBox5 Name TextBox5 DataField TextBox5 DataSource Command Button1 Name Command Button1 Caption Command Button2 Name Command Button2 Caption Command Button3 Name Command Button3 Caption Command Button4 Name Command Button4 Caption Command Button5 Name Command Button5 Caption Command Button6 Name Command Button6 Caption Command Button7 Name Command Button7 Caption 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: a) Click on the ADO control on the form and open up the properties window. lblISBN ISBN: lblPubID Publisher's ID: lblSubject Subject : txtitle Title adoBooks txtPub Year Published adoBooks txtISBN ISBN adoBooks txtPubID PubID adoBooks txtSubject Subject adoBooks cmdSave &Save cmdAdd &Add cmdDelete &Delete cmdCancel &Cancel cmdPrev &< cmdNext &> cmdExit E&xit

Visual Basic
b) Click on the ConnectionString property, the following dialog box will appear.

when the dialog box appear, select the Use Connection String's Option. Next, click build and at the Data Link dialog box, double-Click the option labeled Microsoft Jet 3.51 OLE DB provider.

After that, click the Next button to select the file BIBLO.MDB. You can click on Text Connection to ensure proper connection of the database file. Click OK to finish the connection.Finally, click on the RecordSource property and set the command type to adCmd Table and Table name to Titles.

Visual Basic

Now, you need to write code for all the command buttons. After which, you can make the ADO control invisible. For the Save button, the program codes are as follow: 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 For the Add button, the program codes are as follow: Private Sub cmdAdd_Click() adoBooks.Recordset.AddNew End Sub For the Delete button, the program codes are as follow: Private Sub cmdDelete_Click() Confirm = MsgBox("Are you sure you want to delete this record?", vbYesNo, "Deletion Confirmation") If Confirm = vbYes Then

Visual Basic
adoBooks.Recordset.Delete MsgBox "Record Deleted!", , "Message" Else MsgBox "Record Not Deleted!", , "Message" End If End Sub For the Cancel button, the program codes are as follow: Private Sub cmdCancel_Click() txtTitle.Text = "" txtPub.Text = "" txtPubID.Text = "" txtISBN.Text = "" txtSubject.Text = "" End Sub For the Previous (<) button, the program codes are 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 For the Next(>) button, the program codes are 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

Visual Basic

Visual Basic

Date:__________

Sign:____________