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.

Alfreds FultarV'Sta P'pduct Name Aniseed Eyrup Chartreuse verla L s klca liküö ri Rbssie Eaueryraui E pages-d Vag e -sp re ad

Quantity 6 21 15 15 2 23

Lin=l Fr-ce

S 13.33

513.33

513.33

512.33

543.33

Lxta n d ad

563.33

5273:33

5634.33

524.33

5375.33

Ana I ru o Empa'edadasy he:adps

P'pd jci Kama

Camembert P arut

Ma sea rp p ne Pab:pi

Eingapprean HoKKen Fr.ed

Tofj

Quantity 13 13 5

L'n.t Prie 534.33 532.33 514.33

523.25

Exta n d ad 5343.33 5323.33 573.33

535.75

Antofiio Morenp 1 aquena P'pd jit Kama Aloe Mutton E ostc n Crab Meat Chu co la da Gallost Geitssl

Gumoar Gurnmiparchen Ipph i/offaa

Louisiana Hot Spiced Cica Parth Pastes Que so Cabralas Raclette Courdavaul Rav oii Angelp Rhbnbrau Kk) staler

Adding an ActiveReport to a Visual Studio project To add an ActiveReport to your project

1. Open a new project in Visual Studio.

2. Click on Project > Add New Item.

3. Select ActiveReports file and rename the file rptCalFields.

4. Click Open.

Connecting the data source to a database To connect the data source to a database

1. Click on the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.

3. Select "Microsoft Jet 4.0 OLE DB Provider" and click Next >>.

4. Click on the ellipsis to browse for the access path to Nwind.mdb. Click Open once you have selected the

Quantity

Unit Price

Exte n d ed

13

533.33

5732.33

13

513.43

5134.33

15

512.75

513125

33

52.53

57-5.33

3

52.53

523.33

33

531.23

5336.33

15

543.33

5653.33

4

517.33

565.33

25

532.33

5323.33

53

521.33

51,353.33

15

555.33

5325.33

5

510.53

537.53

33

57.75

5232.53

appropriate access path.

5. Click OK to continue.

6. 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".

7. Click OK to return to the report design surface.

Adding controls to contain data

To add controls to the report

1. Add a GroupHeader/Footer section to rptCalFields.

2. Make the following changes to the group header:

o Change the name to ghProducts o Change the DataField to CompanyName

3. 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

4. Add the following controls to the Detail section:

Control DataField Name

TextBox ProductName TextBox Quantity TextBox UnitPrice

Text/Caption Location OutputFormat txtProductName Product Name 0, 0 (Empty string)

txtQuantity Quantity 2.0625, 0 (Empty string) txtUnitPrice Unit Price 3.437, 0 Currency

TextBox = Quantity * UnitPrice txtExtended

Extended

Currency

Viewing the report To view the report

1. Add the ActiveReports viewer control to a Windows Form.

2. Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports WinForm Viewer for help.

0 0

Post a comment