Professional Documents
Culture Documents
Report Designer
The Microsoft Data Report designer is a versatile data report generator that features the
ability to created banded hierarchical reports. Used in conjunction with a data source such
as the Data Environment designer, you can create reports from several different relational
tables. In addition to creating printable reports, you can also export the report to HTML
or text files.
Possible Uses
• Automatically create reports that are exported in HTML format for instant
distribution on the Internet.
• Create reports that show the sums of transactions occurring on a daily basis.
2. 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
created on a new Toolbox tab named DataReport. 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, or Maximum. For more information about the Function control, see
"Adding a Function Control to the Data Report."
3. Print Preview—Preview the report by using the Show method. The data report is
then generated and displayed in its own window.
Note A printer must be installed on the computer to show the report in print
preview mode.
Topics
• Parts of the Data Report
Sample Application
The sample application named prjNwind corresponds to the project built in the series of
topics that begin with "Creating a Simple Data Report." If you have installed the Visual
Basic sample applications, the file can be found in the Samples directory. If you have not
installed the sample applications, the file can be found on the MSDN™ CD that
accompanies Visual Basic.
3. Data Report Controls—Special controls that only work on the Data Report
designer are included with it. (Note: you cannot use Visual Basic's intrinsic
controls, or any ActiveX controls, on the Data Report designer). These controls
are found in the Visual Basic Toolbox, but they are placed on a separate tab
named "DataReport."
• Page Header—contains information that goes at the top of every page, such as the
report's title.
• Details—contains the innermost "repeating" part (the records) of the report. The
details section is associated 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 or contact name. The Report Footer appears
between the last Page Header and Page Footer.
• Line Control (RptLine)—lets you draw rules on the report to further distinguish
sections.
• Function Control (RptFunction)—a special text box that calculates values as the
report is generated.
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.
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.
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
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.
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
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.
Step by Step
This topic is part of a series that walks you through creating a sample data report.
To See
Go to the next step Extending the Data Report
After compiling a report you may wish to reuse it, either as part of a larger document or
perhaps for distribution on an intranet or the Internet. The Data Report designer's
ExportReport method allows you to accomplish these tasks. Using the ExportReport
method, you can export any report as a text file or as an HTML file. Additionally, you
can use any of a number of ExportFormat objects to tailor the content and appearance of
an exported file.
Important The ExportReport method does not support the exporting of images or
graphic shapes.
ExportFormat Objects
The ExportReport method was designed to work with the ExportFormats collection. Each
ExportFormat object in the collection represents a separate format for the report. For
example, a report formatted for intranet distribution might include names of groups or
employees as part of the report header; for Internet distribution, those same names would
be removed or replaced. You would therefore create at least two ExportFormat objects,
each tailored for the distribution mechanism. However, it is possible to export a report
without creating any ExportFormat objects because four are already provided for you.
By using one of the four members, you can export a report without creating another
ExportFormat object, provided the default meets your requirements. For example, to
export a daily HTML report, you might use the following code:
DataReport1.ExportReport rptKeyHTML
Template Codes
The core of an ExportFormat object is its template. A template is simply a string
containing both the text you want to appear along with constants that represent various
parts of the data report. The constants, values, and descriptions are shown in the table
below:
Constant Value Description
rptTagTitle <!--MSDBRPT_Template_Title--> Represents the title of the
report, as found in the Title
property.
rptTagBody <!--MSDBRPT_Template_Body--> Represents the body of the
report.
To create a simple data report that includes only the name of the author followed by the
body of the report, the template would resemble this:
Dim strT As String
strT = "Author: " & InputBox("Your name") & vbCrLf & rptTagBody
drpNwind.ExportFormats.Add "AuExp", rptFmtText, _
"Author Only Text File", "*.txt", strT
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 PrintReport method. If
an error occurs during printing, trap it in the Error event.
For More Information See "Data Report Events."
Like the standard Visual Basic form, the life of a Data Report designer is marked by
certain key events. Those events, and the order in which they occur, are shown in the
following table:
Event Description
Initialize Occurs after the query has completed, and controls are sited on the
form.
Resize Occurs when the designer is first displayed or when the window
state of an object changes.
Activate Occurs when the designer becomes the active window.
ProcessingTimeout Occurs approximately once every second until all processing has
ended. Use this event to determine if processing has taken too long,
and to cancel the processing.
1. Query—When the data report is first created, a query is sent to the data provider.
Because these methods combine both synchronous and asynchronous processes, there are
separate events to monitor each kind of process.
Note You will understand this topic better if you read the Data Report tutorial first,
beginning at Creating a Simple Data Report.
One way of creating a data report is to:
1. Create a Data Environment designer containing a hierarchy of Command objects.
2. Set the DataSource property of the Data Report designer to the Data
Environment designer.
After retrieving the structure, an appropriate number of Group headers and footers
will be created, and each header/footer pair is assigned a name that corresponds to
a Command object.
5. Drag Command objects from the Data Environment designer to the corresponding
section on the Data Report designer.
All of the data fields contained by a Command object are automatically created on
the data report as TextBox controls on the section where the Command object is
dropped. The DataMember and DataField properties of each TextBox are set in
accordance with the Command object and its data fields.
6. Drag TextBox controls from the section where each was created onto a different
section of the Data Report designer.
When the data report is bound to a data environment, the rules for placement of controls
on the report are not immediately apparent. This topic explains how the hierarchy created
in the data environment relates to the system of group headers and footers constructed in
the data report.
The Data Report designer, on the other hand, is constructed as a series of sections. And
each section can be categorized into one of four types: Report header/footers, Page
header/footers, Group header/footers, and the Detail section. For the purposes of
instruction, we can disregard the Report and Page header/footer pairs. This leaves the
Group headers/footers and the Detail section.
The Detail section, the innermost section of the designer, corresponds to the lowest-level
Command object. As you go up through the hierarchy, the Detail section is bracketed by
pairs of sections, with each pair associated with a single Command object. The figure
below correlates the Command objects with the sections:
Sections Corresponding to Command Objects
Thus the figure shows that the hierarchy of the data environment actually corresponds to
an expanding series of brackets, with the innermost (Detail) section corresponding to the
lowest level of the hierarchy, and the outermost corresponding to the highest-level
Command object.
A second exception for the Function control: it can only be placed in any section pair that
is one level above its own. For example, if the Command 3 object contains a Quantity
field, you can place a Function control that sums the Quantity values onto the footer
section for Command 2, or the footer section for Command 1.
The third exception concerning the Function control is this: unlike the other data-bound
control (the TextBox control), the Function control can be placed in the Report Footer
section. When you do so, the scope of the control's calculation will be increased to
include the whole report. For example, a Function control placed in the Report footer to
calculate the sum of the Quantity field will calculate the sum of every Quantity control on
the report.
Grouping Fields
When you create grouping fields, the Data Environment designer creates two folders
under a single Command object. The first contains the grouping fields, and the second
contains the Detail fields. Even though a new Command object is not created for the
grouping fields, you must create a new group header/footer pair on the data report and
this requirement qualifies it as an exception.
For More Information Details about creating aggregate field can be found in "Creating
Aggregates" in "Using the Data Environment Designer."
Once you have a simple data report, it's easy to extend it and make a more complex
presentation. In the procedure below, the data environment is first extended with the
Order Details and Products tables. The extended report includes the names of products
ordered on a specific date, and resembles the figure below.
Note This topic is part of a series that walks you through creating a sample data report.
It begins with the topic, Creating a Simple Data Report.
5. Right-click the OrderDetails Command object, and click Add Child Command.
Set the properties of the connection as shown below:
Property Setting
Command Name Products
Connection Northwind
DataBase Object Table
Object Name Products
6. Click the Relation tab. The Relate to a Parent Command Object check box
should be checked. The Parent box should contain OrderDetails; both the
Parent Fields and Child Fields/Parameters boxes should contain ProductID.
Click the Add button and then click OK to close the dialog box.
The dialog box allows you to determine if the new header and footer will
"bracket" other header/footer pairs. This becomes important as you add more
header and footer pairs because the outermost pair of header/footers subordinates
all other pairs. This is discussed in greater detail in "Parts of the Data Report."
3. Click OK to select the default placement of the new header and footer pair and
close the dialog box.
4. Select the new group header, and on the Properties window, change its name
from Section1 to Orders_Header. Change the corresponding footer name from
Section4 to Orders_Footer.
6. Click the Detail (Orders_Detail) section to select it. On the Properties window,
change the section's name to Products_Detail.
7. Using the mouse, drag the OrderDate field from the Detail (Products_Detail)
section to the Orders_Header section.
8. From the Data Environment designer, drag the ProductName field (under the
Products command) into the Detail (Products_Detail) section.
10. Resize the group headers, and rearrange the text box controls to resemble the
figure below.
The figure above requires some explanation. First, the Group footers are all closed
in order to take up the least possible space. Like the Details section, any
additional space left in any header or footer will be multiplied in the final report.
Therefore, if a header or footer doesn't contain any fields, you can close the
distance between the headers or footers.
The Group Header named OrderDetails_Header is also closed. If you wonder
why no fields are being shown, you must understand that the Order Details table
in the Northwind database is a join table—the table contains only the IDs of
records from the Orders table joined to IDs of records from the Products table.
Thus the Order Details table doesn't contain fields which are actually displayed.
Instead, it functions only to join two other tables. In the Data Report designer, the
Order Details table therefore functions only to create groups of records—the
product names grouped under the order dates.
Finally, the Details section contains only the names of products. The Details
section contains the innermost level of repeating records.
Step by Step
This topic is part of a series that walks you through creating a sample data report.
To See
Go to the next step Adding a Calculated Field to the Data Report
Start from the beginning Creating a Simple Data Report
A calculated field is a field whose value is calculated as the report is generated. For
example, when estimating the tax on an order, you must multiply the total price by the
local tax rate. But since local tax rates differ and won't be stored in the database, the tax
is generated as the report is created—a calculated field.
In this example, the extended Data Report designer is modified to include a Total field
that calculates the value of Quantity * UnitPrice. Adding a calculated field involves
the following steps:
1. Using a SQL statement in the data environment's Command object to create the
calculated field.
2. Adding three text box controls to the data report: Quantity, UnitPrice, and (for
the calculated field) Total.
The modified data report resembles the figure below.
Note This topic is part of a series that walks you through creating a sample data report.
It begins with the topic, Creating a Simple Data Report.
2. On the General tab, click the SQL Statement button, and add the following
statement to the box:
3. Select OrderID, ProductID, UnitPrice, Quantity, (Quantity *
UnitPrice) As Total From [Order Details]
The SQL statement multiplies the Quantity value by the UnitPrice value to create
the Total value—the calculated field. Also note that the name of the table (Order
Details) contains a space, and must be enclosed by brackets.
6. Delete two Label controls, and change the Caption value of the remaining Label
control to *, and arrange the controls to resemble the figure below:
7. On the Data Report designer, click the UnitPrice text box to select it. On the
Properties window, double-click DataFormat to display the Property Pages
dialog box.
8. In the Format Type box, click Currency. In the Symbol combo box, select the
currency appropriate to your country.
9. Repeat steps 6 and 7 to change the DataFormat property of the Total text box to
Currency.
10. Click the Total text box control to select it. On the Properties window, change the
Alignment property to 1 – rptJustifyRight.
Step by Step
This topic is part of a series that walks you through creating a sample data report.
To See
Go to the next step Adding a Function Control to the Data Report
Start from the beginning Creating a Simple Data Report
The Data Report designer features its own set of controls. Among these, the Function
control has no counterpart among the Visual Basic intrinsic controls and deserves further
explanation.
The Function control displays data that is calculated at run time, using a built-in function,
as the report is generated. A typical example is shown in the figure below, where the
Note This topic is part of a series that walks you through creating a sample data report.
It begins with the topic, Creating a Simple Data Report.
To add a Function control to the data report
1. Using the mouse pointer, click the group footer named Customers_Footer, and
drag it towards the bottom of the window, to create a space between it and the
Orders_Footer footer.
3. Draw the rptFunction control in the space between the two footers.
4. Set the properties of the rptFunction control according to the table below:
Property Setting
DataMember OrderDetails
DataField Total
Name fncTotal
Alignment 1 – rptJustifyRight
DataFormat Currency
5.
8. Click the fncTotal control to select it. On the Properties window, double-click
Font to display the Font dialog box. Change the Font style to Bold.
10. Position the controls on the designer to resemble the figure below:
Step by Step
This topic is part of a series that walks you through creating a sample data report.
To See
Go to the next step Grouping Information in the Data Report
Start from the beginning Creating a Simple Data Report
3. Resetting the DataMember property of the data report to the new Grouping
Command object created in the data environment.
4. Dragging the group field from the data environment to the data report.
Note This topic is part of a series that walks you through creating a sample data report.
It begins with the topic, Creating a Simple Data Report.
6. Click the up arrow three times to insert the group header at the outermost edge of
the header/footer pairs. Click OK to close the dialog box.
7. On the Data Report designer, click the new Group Header to select it, and
change its name from Section1 to Customers_Grouping_Header in the
Properties window. Click the new Group Footer to select it, and change its
name to Customers_Grouping_Footer.
8. Click the Data Report designer's title bar to select the entire data report. On the
Properties window, click DataMember and change the property from Customers
to Customers_Grouping.
When the grouping field was added to the data environment, the equivalent of a
new Command object was also added to the data report. That virtual Command
object is displayed in the drop-down list of data members as
Customers_Grouping.
9. In the Data Environment designer, open Summary Fields in
Customers_Grouping. Drag the Country field into the new section on the Data
Report designer.
10. Delete the Label control that accompanies the Country field. Place the new field
at the leftmost edge of the designer, which should now resemble the figure below:
Step by Step
This topic is part of a series that walks you through creating a sample data report.
To See
Go to the next step Adding an Aggregate Field to the Data Report
Start from the beginning Creating a Simple Data Report
In the Data Environment designer you can also create aggregate fields—fields that
summarize data from a section. An aggregate field is thus similar to the Function control
in that both are calculated as the report is generated. But there are a few differences:
whereas the Function control can only be placed in a Group Footer, an aggregate field
can be placed in any section of the Data Report designer, except the Report
Header/Footer and Page Header/Footer sections.
Another difference is found in how the two fields are created: while the Function control
is a feature of the Data Report designer, the aggregate field is a feature of the Data
Environment designer. The steps to adding an aggregate field to the report are:
2. From the Data Environment designer, drag the aggregate field onto the Data
Report designer.
As an example, the figure below shows an aggregate field that displays the number of
products ordered.
Data Report with Aggregate Field
Using a Function control can be more efficient than an aggregate field. When the data
report calculates the value for a Function control, it takes a certain amount of processing
to create each value. When the data environment creates an aggregate field, it takes a
similar amount of processing. However, you can save that processing time by using the
Function control because the Data Report designer must create the entire report in its own
process.
Note This topic is part of a series that walks you through creating a sample data report.
It begins with the topic, Creating a Simple Data Report.
Now that you have created the aggregate field, you can place it on the Data Report
designer.
3. Reposition and resize the TextBox control and the Label control so the designer
resembles the figure below:
Step by Step
This topic is part of a series that walks you through creating a sample data report.
To See
Go to the next step Grouping Information in the Data Report
Start from the beginning Creating a Simple Data Report