Walkthrough: Calculated Fields
ActiveReports allows you to use a textbox's DataField property to perform calculations based on the value of specific data fields.
This walkthrough illustrates how to create a simple report using calculated fields.
The walkthrough is split up into the following activities:
- Adding an ActiveReport to a Visual Studio project
- Connecting the report to a data source
- Adding controls to the report to contain data
- Viewing the report
To complete the walkthrough, you must have access to the NorthWind database (Nwind.mdb).
When you have finished this walkthrough, you will have a report that looks similar to the following.
Adding an ActiveReport to a Visual Studio project
To add an ActiveReport to your project
- Open a new project in Visual Studio.
- Click on Project > Add New Item.
- Select ActiveReports file and rename the file rptCalFields.
- Click Open.
Connecting the data source to a database
To connect the data source to a database
- Click on the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.
- Click on Build...
- Select "Microsoft Jet 4.0 OLE DB Provider" and click Next >>.
- Click on the ellipsis to browse for the access path to Nwind.mdb. Click Open once you have selected the appropriate access path.
- Click OK to continue.
- In the Query field, type "Select Customers.CompanyName, Products.ProductName, [Order Details].UnitPrice, [Order Details].Quantity FROM Products INNER JOIN ((Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID) ON Products.ProductID = [Order Details].ProductID WHERE (((DatePart("yyyy", [OrderDate])) = 1995)) ORDER BY Customers.CompanyName, Products.ProductName".
- Click OK to return to the report design surface.
Adding controls to contain data
To add controls to the report
- Add a GroupHeader/Footer section to rptCalFields.
- Make the following changes to the group header:
- Change the name to ghProducts
- Change the DataField to CompanyName
- Add the following controls to the GroupHeader section:
Control |
DataField |
Name |
Text/Caption |
Location |
TextBox |
CompanyName |
txtCompanyName |
Company Name |
0, 0 |
Label |
(Empty string) |
lblProductName |
Product Name |
0, 0.3125 |
Label |
(Empty string) |
lblUnitPrice |
Unit Price |
3.437, 0.3125 |
Label |
(Empty string) |
lblQuantity |
Quantity |
2.0625, 0.3125 |
Label |
(Empty string) |
lblExtended |
Extended |
5, 0.3125 |
- Add the following controls to the Detail section:
Control |
DataField |
Name |
Text/Caption |
Location |
OutputFormat |
TextBox |
ProductName |
txtProductName |
Product Name |
0, 0 |
(Empty string) |
TextBox |
Quantity |
txtQuantity |
Quantity |
2.0625, 0 |
(Empty string) |
TextBox |
UnitPrice |
txtUnitPrice |
Unit Price |
3.437, 0 |
Currency |
TextBox |
= Quantity * UnitPrice |
txtExtended |
Extended |
5, 0 |
Currency |
Viewing the report
To view the report
- Add the ActiveReports viewer control to a Windows Form.
- Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports WinForm Viewer for help.
Samples | Walkthroughs
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.