You are on page 1of 13

UNIT-V

Data report Data Environment – Designer - Connection Object - Command Object -Data Report Object –
Methods - Creating a Hierarchical Report.

DATA ENVIRONMENT

In this chapter, we will understand how to use Data Environment, which was introduced in
Visual Basic 6.0, how to use DataCombo, which is a data-bound ActiveX control, and how to
use DataGrid, which is also a data-bound ActiveX control.

Let us understand each of them first. Then we will develop a data-entry screen using all these
three.

DataEnvironment Data Environment object is created and managed using Data Environment
designer. A Data Environment object can accomplish any of the following tasks.

• Can contain connection and command objects


• Can establish relationship between command object to represent hierarchies
• Can group data and get aggregates
• Allows fields to be dragged and dropped on to a Data Report, for report, and a Form for data
entry screen
• Can work as source for Databound control such as DataCombo controls and Hierarchical
Flexgrid control.
In order to work with Data Environment object, you have to add Data Environment Designer to
your project. Data Environment Designer allows programmer to accomplish any of the tasks
mentioned above. Data Environment Designer is invoked when you double click on Data
Environment object in Project Explorer.

To add Data Environment object to the project:

1. Select Project-> Add Data Environment. If Add Data Environment option is not found then
select More ActiveX Designers and then select Data Environment.

Note: Visual Basic adds Data Environment Designer to your project and also creates a
connection object.

DATA ENVIRONMENT DESIGNER

Working with Connection Object


Whenever you create a Data Environment object a Connection object is added to Data
Environment object. But that Connection object is not connected to any database. So first we
have to establish a connection with the Connection object. Now, let us see how to connect to
database through Connection object.

To connect to Biblio.mdb using connection object:

1. Select Connection object (Connection1) and click on right button.


2. From popup menu select Properties option
Data Environment designer displays Data Link properties dialog.
3. Select the required OLEDB provider (Microsoft Jet 3.51 OLE DB Provider)
4. Select Connection tab and select the name of the database ( BIBLIO.MDB)
5. Click on Ok
To rename connection object:

1. Invoke popup menu by click on Connection object with right button.


2. Select Rename option.
3. Enter new name and press enter key to confirm it.

CREATING COMMAND OBJECT


After connection is established using Connection object, now we have to create a command
object to access the data of the database. A connection object can contain any number of
command objects. In fact, a command object can contain another command object, which is
called as child command object.

To create command object to get data from Authors table:

1. Invoke popup menu of Connection object and select Add Command option
Data Environment Designer creates a command object with name Command1.
2. Invoke Popup menu of command object by clicking on right button and select Properties
option.
Properties dialog is displayed
3. Change the Name of the command object to Authors
4. Select Table as Database Object
5. Invoke the list of tables by clicking on down arrow of Object Name drop down and select
Authors as the name of the table.
6. For the time being, ignore remaining tabs and click on OK.

Properties of Command object.

A command object is created with the default name (command1). If you expand the command
object by clicking on + sign, you get the columns of AUTHORS table.

Data Environment designer after a command object is created.

To create master-detail relationship:

One of the interesting features of Data Environment object is, it allows you to create hierarchy of
objects.

To understand how to create hierarchy of command objects, let us create two command objects.
One for Publishers table and another for Titles table.

Publishers command object becomes parent command and Titles command object becomes child
command object. These hierarchies can be used straightaway in Hierarchical FlexGrid control
and Data Report object.

To create command object for Publishers:

1. Select connection object, Biblio, and click on right button to invoke popup menu and select
Add Command option of popup menu.
2. After a command object is created invoke its properties by selecting Properties option from
Popup menu.
3. Change Name of the command object to Publishers
4. Choose Table as the Database Object and select Publishers as the name of the object.
5. Click on Ok.

To create child command object for Titles:

1. Select Publishers command object and invoke popup menu.


2. Select Add Child Command option.
3. A new command object is created and placed along with fields of Publishers command object.
4. Select Child command object and invoke its properties
5. Change name to Titles.
6. Choose Table as Database Object and Titles as the name of the database object.
7. Click on Relation tab to set relationship between Publishers and Titles command object.
8. Make sure Parent Fields and Child Fields are set to PubID and click on Add button to establish
relationship between Publishers and Titles based on PubId field.
9. Click on Ok.

After child object is created, Data Environment Designer has three command objects – Authors,
Publishers, and Titles. Where Authors and Publishers are at the same level (Parent objects),
Titles is a child command object of Publishers command object. When you expand all command
objects, Data Environment Designer

Data Environment Designer after three command objects are added.

Data Combo and Data List controls


DataCombo and DataList controls are used to present a list of values to users from which user
can select one. The list of values to be displayed to user will come from one recordset and the
selected value goes into another recordset.

Note: Whenever I refer to DataCombo, it also refers to DataList, unless the difference is
explicitly specified.

As DataCombo is an ActiveX control, it is to be loaded into its project explicitly. And


DataCombo is based on OLEDB interface. So it can be used with either Data Environment or
ActiveX data control, because they are also based on OLEDB (use ADO).

To load Data List controls:

1. Select Project->Components
2. Visual Basic displays a list of available ActiveX controls.
3. Turn on check box on the left of Microsoft DataList Controls 6.0 (OLEDB)
4. Click on OK.

Two ActiveX controls are loaded into project – DataList and DataCombo.

Note: You also have Microsoft Data Bound List Controls 6.0 ActiveX control. It contains
DBList and DBCombo, which work with Data Control and not with OLEDB. If you are
developing a project with only DAOs or RDOs, then you can use these controls.

The following are the important properties that are specific to Data List controls (Data Combo
and Data List).

Property Meaning
BoundColumn Contains the name of the source field in the Recordset object that is used to
supply a data value to another Recordset to which control is bound.
ListField Returns or sets the name of the field in the Recordset object, specified by the
RowSource property, used to fill the DataCombo or DataList control's list portion.
RowSource Sets a value that specifies the source from which the control’s list is filled. The
source is either an ActiveX Data control or Data Environment.
RowMember Returns or sets the data member to be used to display list text. Used when
RowSource is a Data Environment.
BoundText Returns or sets the value of the field specified by the BoundColumn property.
DataSource Contains an object to which control is bound. It is generally ADODC or Data
Environment.
DataMember Specifies the data member of Data Environment to which control is bound.
DataField Specifies the name of the field to which control is bound.
DataChanged Determines whether data in the control has been changed by any process.
Style Determines the behavior and appearance of the control. Applicable only to DataCombo.
MatchedWithList Returns True if the current content of the BoundText property matches one of
the records in the list portion of the control.
SeletedItem Returns a value containing a bookmark for the selected record in a DataCombo or
DataList control.
VisibleItems Returns an array of bookmarks, one for each visible item in the DataCombo or
DataList control's list.
VisibleCount Returns a value indicating the number of visible items in the list portion of the
DataCombo or DataList control.
MatchEntry Returns or sets a value indicating how the DataCombo or DataList control performs
searches based on user input.
OBJECT DESIGNER METHODS
An object is a type of user interface element you create on a Visual Basic form by using a
toolbox control. In fact, in Visual Basic, the form itself is an object. Every Visual Basic control
consists of three important elements −
• Properties which describe the object,
• Methods cause an object to do something and
• Events are what happens when an object does something.
Control Properties
All the Visual Basic Objects can be moved, resized or customized by setting their properties. A
property is a value or characteristic held by a Visual Basic object, such as Caption or Fore Color.
Properties can be set at design time by using the Properties window or at run time by using
statements in the program code.

Object. Property = Value


Where
• Object is the name of the object you're customizing.
• Property is the characteristic you want to change.
• Value is the new property setting.
For example,

Form1.Caption = "Hello"

You can set any of the form properties using Properties Window. Most of the properties can be
set or read during application execution. You can refer to Microsoft documentation for a
complete list of properties associated with different controls and restrictions applied to them.
Control Methods
A method is a procedure created as a member of a class and they cause an object to do
something. Methods are used to access or manipulate the characteristics of an object or a
variable. There are mainly two categories of methods you will use in your classes −
• If you are using a control such as one of those provided by the Toolbox, you can call any
of its public methods. The requirements of such a method depend on the class being used.
• If none of the existing methods can perform your desired task, you can add a method to a
class.
DATA REPORT
Introduction to data report

Helps you design a report that displays fields and records from the underlying table or query.
A report is an effective way to present data in a printed format. You can display the information
the way you want to see it
You create the report using graphical object called Data report designer controls. Data report
designer controls. Include: a data-bound Textbox control, a function control which displays
calculated figures, and Image control for inserting graphics, labels that display captions, and a
line and a shape control that graphically organizes the data.
Note: Although the data report designer controls are similar to Visual Basic intrinsic controls,
data repot designer controls have a limited subset of features. When the Data report designer is
added to a project, the designer’s controls are placed in the Visual Basic toolbox on a new tab
named data report and can be used only in the Microsoft data report.
Creating repots is a main function of any good business application. A system might have useful,
data, but without a coherent way to present it, the numbers are meaningless. So, Microsoft
provides a Data Report designer. Which is used to generate eye catching reports. It is used in
conjunction with a data source such as the data environment designer.
The Data Report generates reports using records from database. To use it.
1. Configure a data source, such as the Microsoft data environment, to access a database.
2. set the Data member property of the data report object to a data member.
3. set the data member property of the Data Report.
4. Right click the designer and click retrieve structure,
5. Add appropriate controls to the appropriate sections.
6. Set the data member and Data field properties for each control.
7. At run time, use the show method to display the data report.
Use the data report object to programmatically change the appearance and behavior of the data
report by changing the layout of each section object.
The data report designer also features the ability to export reports using the export report method.
This method allows you specify an export format object, from the export formats collection, to
use as a template \for the report.
Part of the Data Report
The Data Reporter designer consists of the following objects:
Data Report Object – Similar to a Visual Basic form, the Data Report object has a visual
designer. It is used to create the layout of a report.
Section object – The Data report designer contains sections which helps to configure the report
more elegantly. The sections of the Data report designer is explained in the next section.
Data report controls – special controls that only work on the data report designer are included
with it. These controls are found in the Visual Basic toolbox, but they are placed on a separate
tab named ―Data Report’
Report Designer – User the report Designer to create and modify reports. When the report
designer window is active, Visual Basic display report controls toolbar.
Data report designer features – the data report designer has several features:

Drag and drop functionality for fields – When fields are dragged from the Microsoft data
environment designer to the data report designer, Visual Basic automatically create a text box
control on the data report and sets the Data member and Data Field Properties of the dropped
field. A command object can also be dragged from the Data environment designer to the data
report designer. In that case, for each of the fields contained by the command object, a text box
control with be created on the date report; the Data member and Data field property for each text
box will be set to the appropriate values
Toolbox controls – The data report designer features its own set of controls. When a Data report
designer is added to a project, the controls are automatically crated on a new toolbox tab named
Data report. Most of the controls are functionally identical to Visual Basic intrinsic controls, and
include a label, shape, image, textbox, and line control. The sixth control, the function control,
automatically generates one of four kinds of information: sum, average, minimum, of maximum.
Print Report – When a report is generated, it can be printed by clicking the printer icon on the
toolbar.
Note: A printer must be installed on the computer to print a report

File export – in addition to creating printable reports, it can be exported to text files, by clicking
the export icon and specifying text file name.
Section of the Data Report Designer
The Data report designer contains the following sections:
Report Header – Contains the text that appears at the very beginning of a report, such as the
report title, author, or database name.
Page Header – Contains information that goes at the top of every page, such as the report’s title.
Group Header/Footer – contains a ―repeating‖ section of the data report. Each group header is
matched with group footer. The header and footer pair are associated with a single command
object in the data environment designer.
Details- Contains the innermost ―repeating‖ part (the records) of the report. The details section is
associate with the lowest level command object in a data environment hierarchy.
Page Footer – Contains the information that goes at the bottom of every page, such as the page
number.
Report Footer – Contains the text that appears at the very end of the report, such as summary
information, or an address of contact name.
Data report controls – When a new data report designer is added to a project, the following
controls are automatically placed in the toolbox tab named data report.
Printing a Data Report
Printing a data report can be accomplished in one of two ways. The user can click the print
button that appears on the data report in print preview mode (using the show method), or you can
programmatically enable printing using the print report method. If an error occurs during
printing, trap it in the error event.
Choosing to display a print dialog box
When printing a report programmatically, you have two choices: to print by displaying the print
dialog box or by printing without displaying the dialog box.
To display the print dialog box.
1. Add a command button to a form.
2. In the button’s click event, place the following code: data report1. print report true.
The print dialog box allows the user to select a printer, print to file, select a range of pages to
print, and specify the number of copies to print.
Note: Printers must be installed on the computer in order to present a choice of printers
Printing without a Dialog Box
In some cased, you may wish to print the report without user intervention. The print report
method also gives you the option of selecting a range of pages to print, either all, or a specified
range.
To print without displaying the dialog box
1. Add a command button to a from
2. In the button’s click event, place the following code:
Data report1. Print report false
Or, to specify a range of pages to print, use the code below:
0 Data report1 print report false, rpt created with the data report designer.
Print Report Method
At run time, prints the data report created with the data report designer.
Syntax
Object. Print report (Show Dialog, Range, and Page From, Page To)
The print report method syntax has these parts:
Past description object required. And object expression that evaluates to an object in the applies
to list. Show dialog Optional. A Boolean expression that determines if the print dialog box is
shown. Range optional. Sets an integer that determines if all the pages in the report will be
executed or a range of page, as shown in setting. Page from optional. An integer that sets the
page from which to start printing. Page to optional. An integer that sets the page at which to stop
printing.

HIERARCHICAL REPORT
The Data Report Designer supports hierarchical data commands that return a recordset
containing a child recordset, which is related to the parent by a key. You can create reports from
these recordsets and perform grouping operations. Hierarchical data commands are based on the
hierarchical cursor feature in ADO 2.0 and the Data Environment Designer.
Let's examine Data Report Designer. First, it doesn't hook directly into a database table or use
SQL to pull its data. Instead, each report has a DataSource property that lets you point at an
object that will return an ADO recordset, allowing different data sources to feed data into one
report.
To create the reports, you use the Data Report Designer interface, which uses a traditional
banded report writer format, as you see in Screen 1. Each report part (e.g., headings, footers, and
details) appears in a separate band of the report. The bands in Screen 1 are labeled Page Header,
Detail, and Page Footer and change as you add groups that cause repeating sections in the data.
For example, if you group the information to display customer order data for individual
customers, Data Report Designer will add a Group Header and Group Footer. The Page Header
and Page Footer bands contain each page's header and footer. Additionally, you can create a
separate header and footer for the entire report by adding a Report Header and Report Footer.
Usually the innermost band in the report, the Detail band does most of the work and repeats for
each detail record in the recordset. If you have several grouping bands, the Detail band sits in the
middle and represents the output data.
In design mode, the Data Report Designer works similarly to a VB form (i.e., after adding a
report to a project, you can edit the Data Report Designer report or use the Properties window to
change the report's properties). Also, the Data Report Designer adds a special DataReport tab to
the Toolbox. This tab contains controls that work only in reports. You can link the data-bound
controls to the recordset that drives the report. However, only during the design phase can you
set the other controls, which add information or design to the report.
You can add as many Data Report Designers to a project as you need. Each Data Report
Designer represents one report and shows up in the Designers folder in Project Explorer.
To add a Data Report Designer to your project, select Add Data Report from the Project menu. If
the Designer folder doesn't exist, this command will create it and put a Data Report Designer in
it. You can open the Data Report Designer by double-clicking the report name in the Designer's
folder.
Creating a Report

Before you create a report, create the report's DataSource. The DataSource can be an ADO
recordset, data class, or Data Environment data command. In this example, I used the Pubs
sample database that ships with SQL Server. I added a Data Environment to the project, then
added a data connection to the Pubs database. Next I created a new data command (Authors) and
set its SQL text (CommandText property) to:
SELECT au_id, au_lname, au_fname, phone,
address, city, state, zip, contract
FROM authors
ORDER BY au_lname, au_fname

Close the command to update the properties. Now, add a report to your project (Project, Add
Data Report). Next, click the report name in Project Explorer and press F4 to open the report's
properties. Now, set the report's properties that determine its interaction and appearance. For this
example, we'll change only the most frequently used properties.
First, give the report a descriptive name such as rptAuthors, and select the Data Environment
containing your data command as the DataSource. This section will link the report to the Data
Environment. Next, select the data command (Authors) for the DataMember property. These
steps are necessary to link the report to a data source. Next, enter a report title string (Author
Listing) in the Caption property. The other default values will work until you begin to fine-tune
your report.
Now, save the project. The report file will default to the same file name as its name in the
project. The extension for the file is .dsr, which is used for all designers. To avoid conflicts with
other designers, such as the Data Environment, prefix your reports with a standard naming
convention such as rpt.
Next, open the report designer by double-clicking its name. When you assign a data command to
the report, set up the report's default structure to match the recordset that the command returns.
Right-clicking the report designer and selecting Retrieve Structure from the shortcut menu is the
easiest way to achieve this match. If you've already created a report, don't use this command,
because it'll recreate the structure and destroy your settings. When the command finishes, the
report's structure—Page Header, Detail, and Page Footer bands—will match your command. For
this example, that structure consists of Page Header, Detail, and Page Footer bands.
Now, let's create the report. This process is like building a VB form. You drag the special Data
Report controls onto the Data Report Designer for the report you're working on. Your data
command provides the easiest way to add controls to the report. Open the Data Environment
containing the data command the report uses. Size the window so that you can see the Data
Environment Designer and Data Report Designer at the same time. To add label and textbox
controls for each recordset field that the data command returns, drag and drop the data command
onto the Detail area of the Data Report Designer. The format will resemble a table, but you can
quickly change that format.
Drag the first field's label to the Page Header, and drop it into the position where you want the
field. Now, drag the textbox that goes with that label and drop it under the Detail section.
Perform this step for each field in the recordset. Using this technique, I created the report I
designed in Screen 1. Notice how the header fields line up with the detail fields.
Before you continue with this report work, you can add code to the application to test it. Add a
form to your project, then add a Command button and name it cmdAuthorReport. Double-click
the command button you just added to open the code editor. In the Click event for the button, add
this code:
rptAuthors.Show
The report object's Show method will open the report in a preview window. Screen 2 shows the
completed sample report (rptAuthors). The report window lets users review the report, print it,
and export it to an htm or ASCII file.
You can print the report directly by calling the PrintReport method, which takes several
parameters. If you execute the method with no parameters, a dialog will prompt you for
information about the report. The parameters are:
• ShowDialog—a Boolean expression that determines whether to display the Print dialog
box. The default value, True, causes the dialog to show.
• Range—an integer value that determines which pages to print. The default,
rptRangeAllPages (0), causes all pages to print. You can use rptRangeFrom (1) and the next two
parameters to specify which pages to print.
• PageFrom—an integer value that determines the first page to print.
• PageTo—an integer value that determines the final page to print.
In addition to PrintReport, you can call the ExportReport method to export the report to a file.
Also, you can pass this method parameters to alter its behavior.
So far, you can see how the report creates simple layouts and hooks them into your application.
The Image, Line, and Shape controls let you add graphic features to a report. I added the lines
under the field titles in Screen 2 to the Page Header just below the fields. Then I copied this line
to the clipboard, pasted it into the Detail area, aligned it just under the fields, and sized the
bottom of the Detail area to just below the line. These two lines generated the results seen in
Screen 2.
The Data Report Designer is an important new reporting tool for database applications. It's easy
to set up, but lets you produce well-designed reports. The report object also lets you control
report display or printing, or export reports from your code to htm or Text files, making the
reports usable in numerous situations.

You might also like