Using The ADO Data Control
Using The ADO Data Control
This topic creates a simple data report using a Data Environment designer as a data source. The Data Environment designer uses the NorthWind
database supplied with Visual Basic to create a simple hierarchical cursor. The cursor contains two tables, Customers and Orders, and uses the
CustomerID field to link the two. The finished report resembles the figure below.
Simple Data Report: Order Dates by Customers
Before you begin the step-by-step process, ensure that the Northwind database (Nwind.mdb) is present on your computer. If it is not present, copy
the file from your Visual Basic CD onto your hard disk.
To create a simple hierarchical cursor in the Data Environment designer
2. On the Project menu, click Add Data Environment to add a designer to your project. If the designer is not listed on the Project menu,
click Components. Click the Designers tab, and click Data Environment to add the designer to the menu.
Note The first four kinds of ActiveX designers loaded for a project are listed on the Project menu. If more than four designers are
loaded, the later ones will be available from the More ActiveX Designers submenu on the Project menu.
3. On the Data Link Properties dialog box, click Microsoft Jet 3.51 OLE DB Provider. This selects the correct OLE DB provider for
accessing a Jet database.
5. Click the ellipsis button (…) next to the first text box.
6. Use the Select Access Database dialog box to navigate to the nwind.mdb file, which is installed in the Program Files\Microsoft Visual
Studio\Vb98 directory.
8. Right-click the Connection1 icon, and click Rename. Change the name of the icon to Northwind.
9. Right-click the Northwind icon, and then click Add Command to display the Command1 dialog box. In the dialog box, set the properties
as shown below:
Property Setting
Connection Northwind
10.
11. Click OK to close the dialog box.
12. Right-click the Customers command, and click Add Child Command to display the Command2 dialog box. In the dialog box, set the
properties as shown below:
Property Setting
Connection Northwind
13.
14. Click the Relation tab. The Relate to a Parent Command Object check box should be checked. The Parent box should
contain Customers; both the Parent Fields and Child Fields/Parameters boxes should containCustomerID.
When designing relational databases, it's customary for related tables to use the same name for linking fields. In this case, the linking
fields are both named CustomerID. The Data Environment designer automatically matches such pairs in the dialog box.
Clicking the Add button adds the relation to the Command object. After closing the dialog box, the Data Environment designer reflects
the relationship by displaying the two commands as a hierarchy. This hierarchy will be used to create the data report.
16. Set the properties of the project and designer according to the settings below, then save the project:
Object Property Setti
17.
1. On the Project menu, click Add Data Report, and Visual Basic will add it to your project. If the designer is not on the Project menu,
click Components. Click the Designers tab, and click Data Report to add the designer to the menu.
Note The first four kinds of ActiveX designers loaded for a project are listed on the Project menu. If more than four designers are
loaded, the later ones will be available from the More ActiveX Designers submenu on the Project menu.
2. Set the properties of the DataReport object according to the table below:
Property Setting
Name rptNwind
3. On the Properties window, click DataSource and then click deNwind. Then click DataMember and click Customers.
Important To set the DataSource property to deNwind, the Data Environment designer must be open. If it is closed, press CTRL+R to
display the Project window, then double-click the data environment icon.
You have added a new group section to the designer. Each group section has a one-to-one correspondence to a Command object in the
data environment; in this case, the new Group section corresponds to the Customers Command object. Notice also that the Group Header
has a matching Group Footer section.
Note The Data Environment allows you to create hierarchies of Command objects wherein a Command object has more than one child
object — child Command objects parallel to each other. The Data Report designer, however, is not as flexible, and can't display more than
one child object at a time. In such cases, when executing a Retrieve Structure command, the Data Report will display only the first of the
child commands, and none below it. Thus you should avoid creating Command hierarchies with parallel children commands.
5. From the Data Environment designer, drag the CompanyName field (under the Customers command) onto the Group Header
(Customers_Header) section.
The Group Header section can contain any field from the Customers command, however, for demonstration purposes, only the Customer
name is displayed at this time.
If you do not want a Label control to be included with the TextBox control, you can uncheck the Drag and Drop Fields Caption option on
the Field Mapping tab of the Data Environment designer's Options dialog box.
7. From the Data Environment designer, drag the OrderDate field (under the Orders command) onto the Details (Orders_Detail) section.
Delete the Label control.
The Details section represents the innermost "repeating" section, and thus corresponds to the lowest Command object in the Data
Environment hierarchy: the Orders Command object.
8. Resize the Data Report designer's sections to resemble the figure below:
It's important to resize the height of the Details section to be as short as possible because the height will be multiplied for every
OrderDate returned for the CompanyName. Any extra space below or above the OrderDate text box will result in unneeded space in the
final report.
1. On the Project Explorer window, double-click the frmShowReport icon to display the Form designer.
When you add a Data Report designer to your project, its controls are added to the tab named DataReport. To use the standard Visual
Basic controls, you must switch to the General tab.
4. Set the properties of the Command1 control according to the table below:
Property Setting
Name cmdShow
5.
6. In the button's Click event, paste the code below.
7. Private Sub cmdShow_Click()
8. rptNwind.Show
End Sub
10. Click Show Report to display the report in print preview mode.
Note If you use this method, you can remove the Form object from your project.
You have learned how to build a database in Visual Basic 6 in previous chapters, however you have not learned how
to display the saved data in a report. Reports are important and useful in many respects because they provide useful
and meaningful information concerning a set of data. In this chapter, we will show you how to create a report in Visual
Basic 6.
In previous versions of Visual Basic 6, there is no primary reporting . Previous versions of Visual basic 6 uses Crystal
Reports tool, a software from Seagate. Fortunately, Microsoft has integrated a good report writer into Visual Basic 6,
so you no longer need to use Crystal Report.
Start Visual Basic as a Standard EXE project. From the Project menu in the VBE, select Add Data Report in the dropdown menu.
Now, you will be presented with the data report environment, as shown in Figure 40.1. The data report environment contains six
controls, they are RptTextBox, RptLine, RptFunction, RptLabel, RptImage and RptShape.
You can customize your report here by adding a title to the page header using the report label RptLabel. Simply drag and draw the
RptLabel control on the data report designer window and use the Caption property to change the text that should be displayed. You
can also add graphics to the report using the RptImage control.
Click the Project menu, then select Data Environment. from the drop-down menu. The default data environment will appear, as shown in
figure 40.2
Figure 40.2: Data Environment
Now, to connect to the database, right-click connection1 and select Microsoft Jet 3.51 OLE DB Provider (as we
are using MS Access database) from the Data Link Properties dialog (as shown in Figure 40.3), then click next.
Figure 40.3
Now, you need to connect to the database by selecting a database file from your hard disk. For demonstration
purpose, we will use the database BIBLIO.MDB that comes with Visual Basic, as shown in Figure 40.4. The path to
this database file is C:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB. This path varies from computers to
computers, depending on where you install the file. After selecting the file, you need to test the connection by clicking
the Test Connection button at the right bottom of the Data Link Properties dialog. If the connection is successful, a
message that says 'Test Connection Succeeded' will appear. Click the OK button on the message box to return to the
data environment. Now you can rename connection1 to any name you like by right-clicking it. For example, you can
change it to MyConnection. You may also change the name of DataEnvironment1 to MyDataEnvironment using the
Properties window.
Figure 40.4
In order to use the database in your report, you need to create query to retrieve the information from the database.
Here , we will use SQL command to create the query. First of all, right click on MyConnection to add a command to
the data environment. The default command is Command1, you can rename it as MyCommand, as shown in Figure
40.5.
Figure 40.5: MyCommand
In order to use SQL command, right-click MyCommand and you can see its properties dialog. At the General tab,
select SQL statement and key in the following SQL statement:
This command is to select all the fields from the Authors table in the Biblio.Mdb database. The command ORDER
BY Author is to arrange the list in ascending order according to the Authors' Names.
Now, you need to customize a few properties of your data report so that it can connect to the database. The first
property to set is the DataSource, set it to MyDataEnvironment. Next, you need to set the DataMember property to
MyCommand,as shown in Figure 40.6
Figure 40.6: Properties of
To add data to your report, you need to drag the fields from MyCommand in MyDataEnvironment into MyDataReport,
as shown in Figure 40.7.Visual Basic 6 will automatically draw a RptTextBox, along with a RptLabel control for each
field on the report. You can customize the look of the labels as well as the TextBoxes from the properties window of
MyDataReport.
Figure 40.7
The Final step is to set MydataReport as the Startup form from the Project menu, then run the program. You will see
your report as shown in Figure 40.8. You can print out your report.