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 the 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 the 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.
Before you proceed , you need to create a
database file using Microsoft Access. Here we create a file to store the information of my books and we namedthe table book.
Having created the table, enter a few records, as shown in Figure 26.3 below:
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.
Next click on the Build button and the Data Link
Properties dialog box will appear (as shown Figure 26.4). 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
Figure 26.4: Data Link Properties
Figure 26.5: 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:
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: email@example.com