ActiveReports supports field merged reports using the RichText control. The RichText control can contain field placeholders that are replaceable with their values (merged) at run time.
This walkthrough illustrates how to create a mail-merge report using the RichText control.
This walkthrough is split up into the following activities:
- Adding an ActiveReport to the Visual Studio project
- Connecting the report to a data source
- Adding controls to the report to contain data
- Adding fields to the RichText control
- Using the FetchData event to get information from the data source
- Adding code to update field values in the RichText control for each record
- Adding code to the group header BeforePrint event
- Viewing the report
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB.
When you complete this walkthrough, you will have a report that looks similar to the following:
Adding an ActiveReport to the Visual Studio project
To add an ActiveReport to your project
- Open a new project in Visual Studio.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptLetter.
- Click Open.
Connecting the report to a data source
To connect the report to a data source
- Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
- Click on Build.
- Select Microsoft Jet 4.0 OLE DB Provider and click Next.
- Click the ellipsis button to browse for the access path to the NorthWind database. Click Open once you have selected the appropriate access path.
- Click OK to continue.
- In the Query field, type "SELECT
Customers.CustomerID, Customers.CompanyName,
Customers.ContactName, Customers.Address,
Customers.City, Customers.Region, Customers.Country, Customers.PostalCode, Orders.OrderID, Orders.OrderDate, [Order Subtotals].Subtotal
FROM Customers INNER JOIN ([Order Subtotals] INNER JOIN Orders ON [Order Subtotals].OrderID = Orders.OrderID) ON Customers.CustomerID = Orders.CustomerID". - Click OK to return to the report design surface.
Adding controls to the report to contain data
To add controls to the report
- Add a GroupHeader/Footer section to rptLetter by right-clicking the design surface of the report and selecting Insert > Group Header/Footer.
- Make the following changes to the group header:
- Change the Name property to ghCustomerID
- Change the DataField property to CustomerID
- Change the ColumnLayout property to False
- Change the KeepTogether property to True
- Make the following changes to the group footer:
- Change the Name property to gfCustomerID
- Change the ColumnLayout property to False
- Change the KeepTogether property to True
- Change the NewPage property to After.
- Add the following controls to the PageHeader section:
Control Name Text Location Picture imgLogo 3.8, 0 Label lblNorthWind NorthWind 4.8, 0 Label lblTraders Traders 4.8, 0.5 - Add the following controls to the GroupHeader section:
Control DataField Name Text Miscellaneous Location RichText rtf 0, 0 Textbox Subtotal txtTotalOrders Visible = False OutputFormat = Currency
SummaryType = SubTotal
SummaryGroup = ghCustomerID
5.437, 0.0625 Label lblOrderID Order ID 0.875, 2.25 Label lblOrderDate Order Date 1.875, 2.25 Label lblAmount Amount 4.375, 2.25 - In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section and set the following properties of each textbox as indicated.
Field Text Miscellaneous Location OrderID Order ID Alignnent: Right 0.8125, 0 OrderDate Order Date OutputFormat = MM/dd/yy 1.875, 0 Subtotal Subtotal OutputFormat = Currency Alignment: Right
4.3125, 0 - Add the following controls to the GroupFooter section:
Control Name Text Location Label lblYours Yours, 0.1875, 0.125 Label lblNTAP Accounts Receivable 0.1875, 0.4375 - Add the following controls to the PageFooter section:
Control Name Text Location Label lblNWAddress NorthWind Traders, One Portals Way, Twin Points WA 98156 0, 0.0625
Adding fields to the RichText control
To add fields to the RichText control
- Double-click inside the RichText control box to select it.
- Right-click inside the box and choose Insert Fields.
- Insert the following fields in the following order:
- Date
- CompanyName
- ContactName
- AddressLine
- City
- Region
- Country
- PostalCode
- ContactName
- TotalOrders
- Add the following text to the RichText control box: "Dear [!ContactName], Thank you for your business. Below is a list of your orders for the past year with a total of [!TotalOrders]. Please take this opportunity to review each order and total for accuracy. Call us at 1-800-DNT-CALL with any questions or concerns."
- Your RichText control should be arranged like the following.
Using the FetchData event to get information from the data source
To write the code in Visual Basic
- Right-click in any section of the design window of rptLetter, and click on View Code to display the code view for the report. At the top left of the code view for rptLetter, click the drop-down arrow and select (rptLetter Events). At the top right of the code window, click the drop-down arrow and select FetchData. This creates an event-handling method for rptLetter's FetchData event. Add code to the handler to:
- Retrieve information from the data source
To write the code in C#
- Click in the gray area below rptLetter to select the report. Click on the events icon in the Properties window to display available events for the report. Double-click FetchData. This creates an event-handling method for rptLetter's FetchData event. Add code to the handler to:
- Retrieve information from the data source
The following example shows what the code for the method looks like.
'Visual Basic Dim companyName As String Dim contactName As String Dim addressLine As String Dim city As String Dim region As String Dim country As String Dim postalCode As String Private Sub rptLetter_FetchData(ByVal sender As Object, ByVal eArgs As DataDynamics _ .ActiveReports.ActiveReport3.FetchEventArgs) Handles MyBase.FetchDat companyName = Fields("CompanyName").Value contactName = Fields("ContactName").Value addressLine = Fields("Address").Value city = Fields("City").Value If Fields("Region").Value Is System.DBNull.Value Then region = "" Else region = Fields("Region").Value End If country = Fields("Country").Value postalCode = Fields("PostalCode").Value End Sub //C# string companyName; string contactName; string addressLine; string city; string region; string country; string postalCode; private void rptLetter_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport3 .FetchEventArgs eArgs) { companyName = Fields["CompanyName"].Value.ToString(); contactName = Fields["ContactName"].Value.ToString(); addressLine = Fields["Address"].Value.ToString(); city = Fields["City"].Value.ToString(); if(Fields["Region"].Value is System.DBNull) region = ""; else region = Fields["Region"].Value.ToString(); country = Fields["Country"].Value.ToString(); postalCode = Fields["PostalCode"].Value.ToString(); }
Adding code to update the field values in the Rich Text control
To write the code in Visual Basic or C#
- Double-click in the GroupHeader section of the report to create an event-handling method for rptLetter's ghCustomerID_Format event. Add code to the handler to:
- Update the field values in the RichText control
The following example shows what the code for the method looks like.
'Visual Basic Private Sub ghCustomerID_Format(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles ghCustomerID.Format Me.rtf.ReplaceField("CompanyName", companyName) Me.rtf.ReplaceField("ContactName", contactName) Me.rtf.ReplaceField("AddressLine", addressLine) Me.rtf.ReplaceField("City", city) Me.rtf.ReplaceField("Region", region) Me.rtf.ReplaceField("Country", country) Me.rtf.ReplaceField("PostalCode", postalCode) Me.rtf.ReplaceField("Date", System.DateTime.Today.Date) End Sub //C# private void ghCustomerID_Format(object sender, System.EventArgs eArgs) { this.rtf.ReplaceField("CompanyName", companyName); this.rtf.ReplaceField("ContactName", contactName); this.rtf.ReplaceField("AddressLine", addressLine); this.rtf.ReplaceField("City", city); this.rtf.ReplaceField("Region", region); this.rtf.ReplaceField("Country", country); this.rtf.ReplaceField("PostalCode", postalCode); this.rtf.ReplaceField("Date", System.DateTime.Today.Date.ToString()); }
Adding code to the Group Header BeforePrint Event
To write the code in Visual Basic
- Right-click in any section of the design window of rptLetter, and click on View Code to display the code view for the report. At the top left of the code view for rptLetter, click the drop-down arrow and select ghCustomerID. At the top right of the code window, click the drop-down arrow and select BeforePrint. This creates an event-handling method for rptLetter's ghCustomerID_BeforePrint event.
To write the code in C#
- Click in the GroupHeader section of rptLetter to select it. Click on the events icon in the Properties window for ghCustomerID to display available events for the section. Double-click BeforePrint. This creates an event-handling method for rptLetter's ghCustomerID_BeforePrint event.
The following example shows what the code for the method looks like.
'Visual Basic Private Sub ghCustomerID_BeforePrint(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles ghCustomerID.BeforePrint Me.rtf.ReplaceField("TotalOrders", Me.txtTotalOrders.Text) End Sub //C# private void ghCustomerID_BeforePrint(object sender, System.EventArgs eArgs) { this.rtf.ReplaceField("TotalOrders",this.txtTotalOrders.Text); }
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 Windows Form Viewer for help.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |