0% found this document useful (0 votes)
74 views

Using The ADO Data Control

This document provides instructions for creating a simple data report in Visual Basic using a data environment designer and Northwind database. It involves creating a hierarchical cursor from two tables - Customers and Orders - linked by CustomerID. The report displays customer names in group headers and order dates in details. Code is added to preview the report using the Show method when a button is clicked.

Uploaded by

Ajinkya Bhagat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Using The ADO Data Control

This document provides instructions for creating a simple data report in Visual Basic using a data environment designer and Northwind database. It involves creating a hierarchical cursor from two tables - Customers and Orders - linked by CustomerID. The report displays customer names in group headers and order dates in details. Code is added to preview the report using the Show method when a button is clicked.

Uploaded by

Ajinkya Bhagat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Creating a Simple Data Report

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

1. Create a new Standard EXE project.

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.

4. Click the Next button to get to the Connection tab.

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.

7. Click OK to close the dialog box.

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

Command Name Customers

Connection Northwind

DataBase Object Table

Object Name Customers

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

Command Name Orders

Connection Northwind

DataBase Object Table

Object Name Orders

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.

15. Click Add. Click OK to close 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

Project Name prjNw

DataEnvironment Name deNw

Form Name frmS

17.

Creating the Data Report


Once the Data Environment designer has been created, you can create a data report. Because not all of the fields in the data environment will be
useful in a report, this series of topics creates a limited report that displays only a few fields.
To create a new data report

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

Caption Northwind Data Report

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.

4. Right-click the Data Report designer, and click Retrieve Structure.

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.

6. Delete the Label control (rptLabel) named Label1.

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.

9. Save the project.

Preview the Data Report Using the Show Method


Now that the data environment and the data report objects have been created, you are almost ready to run the project. One step remains: to write
code to show the data report.
To show the data report at run time

1. On the Project Explorer window, double-click the frmShowReport icon to display the Form designer.

2. On Toolbox, click the General tab.

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.

3. Click the CommandButton icon and draw a CommandButton on the form.

4. Set the properties of the Command1 control according to the table below:
Property Setting

Name cmdShow

Caption Show Report

5.
6. In the button's Click event, paste the code below.
7. Private Sub cmdShow_Click()
8. rptNwind.Show
End Sub

9. Save and run the project.

10. Click Show Report to display the report in print preview mode.

Optional—Setting the Data Report as the Startup Object


You can also display the data report with no code at all.

1. On the Project menu, click prjNwind Properties.

2. In the Startup Object box, select rptNwind.

3. Save and run the project.

Note If you use this method, you can remove the Form object from your project.

Lesson 40 : Creating Reports in Visual


Basic 6
<Previous Lesson> <<Home>>

40.1 A brief introduction to reporting tool in Visual basic 6

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.

40.2 Steps in building your report in Visual Basic 6


Visual Basic 6 provides you with a data report designer to create your report, it is somewhat similar to data report
designer in Microsoft Access. The data report designer has its own set of controls which allow you to customize your
report seamlessly. The steps in creating the report in VB6 are listed below:

Step 1: Adding Data 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.

Figure 40.1: The Data Report Environment

Step 2: Connecting the report to database using Data Environment Designer

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

Step 3: Retrieving Information from the Database

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:

SELECT Au_ID, Author


FROM Authors ORDER BY Author

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.

Figure 40.8: The Final Report.

You might also like