• Once you have gone to all the trouble of developing and managing a
database, it is nice to have the ability to obtain printed or displayed information
from your data. The process of obtaining such information is known as creating
a data report.
• There are two steps to creating a data report. First, we need to create
a Data Environment. This is designed within Visual Basic and is used to tell the
data report what is in the database. Second, we create the Data Report itself.
This, too, is done within Visual Basic. The Data Environment and Data Report files
then become part of the Visual Basic project developed as a database management system.
• The Visual Basic 6.0 data report capabilities are vast and using them
is a detailed process. The use of these capabilities is best demonstrated by example.
We will look at the rudiments of report creation by building a tabular report
for our phone database.
Example - Phone Directory - Building a Data Report
We will build a data report that lists all the names and phone numbers in our
phone database. We will do this by first creating a Data Environment, then a Data
Report. We will then reopen the phone database management project and add data
Creating a Data Environment
1. Start a new Standard EXE
2. On the Project menu, click Add Data
Environment. If this item is not on the menu, click Components.
Click the Designers tab, and choose Data Environment
and click OK to add the designer to your menu.
3. We need to point to our database. In the Data
Environment window, right-click the Connection1
tab and select Properties. In the Data Link
Properties dialog box, choose Microsoft Jet 3.51 OLE
DB Provider. Click Next to get to the Connection
tab. Click the ellipsis button. Find your phone database (mdb)
file. Click OK to close the dialog box.
4. We now tell the Data Environment what is
in our database. Right-click the Connection1 tab and click Rename.
Change the name of the tab to Phone. Right-click this newly named tab and click
Add Command to create a Command1 tab. Right-click
this tab and choose Properties. Assign the following properties:
Command Name - PhoneList
Connection - Phone
DataBase Object - Table
ObjectName - PhoneList
5. Click OK. All this was needed just to connect
the environment to our database.
6. Display the properties window and give the data environment
a name property of denPhone. Click File and Save
denPhone As. Save the environment in an appropriate folder. We will eventually
add this file to our phone database management system. At this point, my data
environment window looks like this (I expanded the PhoneList tab by clicking the
Creating a Data Report
Once the Data Environment has been created, we can create a
Data Report. We will drag things out of the Data Environment onto a form created
for the Data Report, so make sure your Data Environment window is still available.
1. On the Project menu, click Add Data Report and one will
be added to your project. If this item is not on the menu, click Components. Click
the Designers tab, and choose Data Report and click OK to add the designer to
2. Set the following properties for the report:
Name - rptPhone
Caption - Phone Directory
DataSource - denPhone (your phone data environment - choose, don’t type)
DataMember - PhoneList (the table name - choose don’t type)
3. Right-click the Data Report and click Retrieve
Structure. This establishes a report format based on the Data
4. Note there are five sections to the data report: a Report
Header, a Page Header, a Detail section, a Page Footer, and a Report Footer. The
headers and footers contain information you want printed in the report and on
each page. To place information in one of these regions, right-click the selected
region, click Add Control, then choose the control you wish to place. These controls
are called data report controls and properties are established just like you do
for usual controls. Try adding some headers.
5. The Detail section is used to layout the information you
want printed for each record in your database. We will place two field listings
(Name, Phone) there. Click on the Name tab in the Data Environment window and
drag it to the Detail section of the Data Report. Two items should appear: a text
box Name and a text box Name (PhoneList). The first text box is heading information.
Move this text box into the Page Header section. The second text box is the actual
value for Name from the PhoneList table. Line this text box up under the Name
header. Now, drag the Phone tab from the Data Environment to the Data Report.
Adjust the text boxes in the same manner. Our data report will have page headers
Name and Phone. Under these headers, these fields for each record in our database
will be displayed. When done, the form should look something like this:
In this form, I’ve resized the labels a bit and added
a Report Header. Also, make sure you close up the Detail section to a single line.
Any space left in this section will be inserted after each entry.
6. Click File and Save rptPhone As. Save the environment in
an appropriate folder. We will now reopen our phone database manager and attach
this and the data environment to that project and add capabilities to display
Accessing the Data Report
1. Reopen the phone directory project. Add a command button
named cmdReport and give it a Caption of Show Report. (There may be two tabs in
your toolbox, one named General and one named DataReport. Make sure you select
from the General tools.)
2. We will now add the data environment and data report files
to the project. Click the Project menu item, then click Add File. Choose denPhone
and click OK. Also add rptPhone. Look at your Project Window. Those files should
be listed under Designers.
3. Use this code in cmdReport_Click:
Private Sub cmdReport_Click()
4. This uses the Show method to display the data report.
5. Save the application and run it. Click the Show Report button
and this should appear:
You now have a printable copy of the phone directory. Just click the Printer
icon. Notice the relationship with this displayed report and the sections available
in the Data Report designer.