VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Codes 中文VB About Us

Lesson 26 : Managing Database Using DataGrid Control

In the previous lesson, we use textboxes or labels to display data by connecting them to a database via Microsoft ADO data Control 6.0. The textbox is not the only control that can display data from a database, many other controls in Visual Basic can display data. One of them is the DataGrid control. DataGrid control can be used to display the entire table of a recordset of a database. It allows users to view and edit data.

DataGrid control is not the default item in the Visual Basic control toolbox, you have to add it from the VB6 components. To add the DataGrid control, click on the Project on the menu bar and select components to access the dialog box that displays all the available VB6 components, as shown in the diagram below. Select Microsoft DataGrid Control 6.0 by clicking the checkbox beside this item. Before you exit the dialog box, you also need to select the Microsoft ADO data control so that you are able to access the database. Last, click on the OK button to exit the dialog box. Now you should be able to see that the DataGrid control and the ADO data control are added to the toolbox. The next step is to drag the DataGrid control and the ADO data control into the form.

The components dialog box is shown below:

Figure 26.1: The Components Dialog Box

Figure 26.2: The Design Interface

Before you proceed , you need to create a database file using Microsoft Access. Here we create a database file to store the information of books and we named the table book. Having created the table, enter a few records, as shown in Figure 26.3 below:

Figure 26.3


Next, you need to connect the database to the ADO data control. To do that, right click on the ADO data control and select the ADODC properties, the following dialog box will appear.

Figure 26.4

Next click on the Build button and the Data Link Properties dialog box will appear (as shown Figure 26.5). In this dialog box, select the database file you have created, in my case, the file name is books.mdb. Press test connection to see whether the connection is successful. If the connection is successful, click OK to return to the ADODC property pages dialog box( as shown in Figure 26.4). At the ADODC property pages dialog box, click on the Recordsource tab and select 2-adCmdTable under command type and select book as the table name, then click OK.

Figure 26.5: Data Link Properties

Figure 26.6: Property Pages

Finally you need to display the data in the DataGrid control. To accomplish this, go to the properties window and set the DataSource property of the DataGrid to Adodc1. You can also permit the user to add and edit your records by setting the AllowUpdate property to True. If you set this property to false, the user cannot edit the records. Now run the program and the runtime interface is shown in Figure 26.5 below:

Figure 26.7

Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy