Walkthrough: Rich Text and Field Merging
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 (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.
- Click on Project > Add New Item.
- Select ActiveReports 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 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.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.
- Make the following changes to the group header:
- Change the name to ghCustomerID
- Change the DataField property to CustomerID
- Change the ColumnLayout property to False
- Change KeepTogether property to True
- Make the following changes to the group footer:
- Change the name to gfCustomerID
- Change the ColumnLayout property to False
- Change KeepTogether property to True
- Change the NewPage property to After.
- Add the following controls to the PageHeader section:
Control |
Name |
Text |
Misc Details |
Location |
Picture |
imgLogo |
(Empty string) |
Image = Logo |
4, 0 |
Label |
lblNorthWind |
NorthWind |
Font Size = 14 |
5, 0 |
Label |
lblTraders |
Traders |
Font Size = 14 |
5, 0.25 |
- Add the following controls to the GroupHeader section:
Control |
DataField |
Name |
Text |
Misc Details |
Location |
RichText |
(Empty string) |
rtf |
(Empty string) |
Size = 6, 2.25 in |
0, 0 |
Textbox |
Subtotal |
txtTotalOrders |
(Empty string) |
Visible = False SummaryType = SubTotal SummaryGroup = ghCustomerID |
5, 0 |
Label |
(Empty string) |
lblOrderID |
Order ID |
BackColor = Black ForeColor = White |
0.875, 2.25 |
Label |
(Empty string) |
lblOrderDate |
Order Date |
BackColor = Black ForeColor = White |
1.875, 2.25 |
Label |
(Empty string) |
lblAmount |
Amount |
BackColor = Black ForeColor = White |
4.375, 2.25 |
- Add the following controls to the Detail section:
Control |
DataField |
Name |
Text |
Misc Details |
Location |
Textbox |
OrderID |
txtOrderID |
Order ID |
Align: right |
0.875, 0 |
Textbox |
OrderDate |
txtOrderDate |
Order Date |
OutputFormat = MMMM d, yyyy |
1.875, 0 |
Textbox |
Subtotal |
txtSubtotal |
Subtotal |
OutputFormat = Currency
Align: right |
4.375, 0 |
- Add the following controls to the GroupFooter section:
Control |
Name |
Text |
Location |
Label |
lblYours |
Yours, |
0, 0 |
Label |
lblAR |
Accounts Receivable |
0, 0.5 |
- 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 |
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 Field.
- Insert the following fields in the following order:
- Date
- CompanyName
- ContactName
- Address
- 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.
- Arrange your RichText control like the following example.

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 (Base Class 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 m_companyName As String
Dim m_contactName As String
Dim m_address As String
Dim m_city As String
Dim m_region As String
Dim m_country As String
Dim m_postalCode As String
Private Sub rptLetter_FetchData(ByVal sender As Object, ByVal eArgs As DataDynamics _
.ActiveReports.ActiveReport.FetchEventArgs) Handles MyBase.FetchData
m_companyName = Fields("CompanyName").Value
m_contactName = Fields("ContactName").Value
m_address = Fields("Address").Value
m_city = Fields("City").Value
If Fields("Region").Value Is System.DBNull.Value Then
m_region = ""
Else
m_region = Fields("Region").Value
End If
m_country = Fields("Country").Value
m_postalCode = Fields("PostalCode").Value.ToString
End Sub
//C#
string m_companyName;
string m_contactName;
string m_address;
string m_city;
string m_region;
string m_country;
string m_postalCode;
private void rptLetter_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport _
.FetchEventArgs eArgs)
{
m_companyName = Fields["CompanyName"].Value.ToString();
m_contactName = Fields["ContactName"].Value.ToString();
m_address = Fields["Address"].Value.ToString();
m_city = Fields["City"].Value.ToString();
if(Fields["Region"].Value is System.DBNull)
m_region = "";
else
m_region = Fields["Region"].Value.ToString();
m_country = Fields["Country"].Value.ToString();
m_postalCode = Fields["PostalCode"].Value.ToString();
}
Adding code to update the field values in the Rich Text control
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 Format. This creates an event-handling method for rptLetter's ghCustomerID_Format event. Add code to the handler to:
- Update the field values in the RichText control
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 Format. This creates 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", m_companyName)
Me.rtf.ReplaceField("ContactName", m_contactName)
Me.rtf.ReplaceField("Address", m_address)
Me.rtf.ReplaceField("City", m_city)
Me.rtf.ReplaceField("Region", m_region)
Me.rtf.ReplaceField("Country", m_country)
Me.rtf.ReplaceField("PostalCode", m_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", m_companyName);
this.rtf.ReplaceField("ContactName", m_contactName);
this.rtf.ReplaceField("Address", m_address);
this.rtf.ReplaceField("City", m_city);
this.rtf.ReplaceField("Region", m_region);
this.rtf.ReplaceField("Country", m_country);
this.rtf.ReplaceField("PostalCode", m_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 WinForm Viewer for help.
Samples | Walkthroughs | OutputFormat Strings
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.