ActiveReports supports field merged reports using the RichText control. The RichText control can contain field place holders that can be replaced with 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:
Tip: For basic steps like adding a report to a Visual Studio project and viewing a report, please see the Basic Data Bound Reports walkthrough. |
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\GrapeCity\ActiveReports 6\Data\NWIND.MDB (on a 64-bit Windows operating system, a copy is located in C:\Program Files (x86)\GrapeCity\ActiveReports 6\Data\NWIND.MDB).
When you complete this walkthrough, you will have a report that looks similar to the following:
SQL Query |
Copy Code |
---|---|
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 |
Page header controls
Control | Miscellaneous | Size | Location |
---|---|---|---|
Label | Font Size = 36 Style = Bold Text = GrapeCity |
2.5, 0.65 in | 0, 0 in |
Picture | PictureAlignment = TopLeft Image (click ellipsis, navigate to C:\Program Files\GrapeCity\ActiveReports 6\Introduction (or to C:\Program Files (x86)\GrapeCity\ActiveReports 6\Introduction on a 64-bit Windows operating system) and select itopimage1.gif) |
4.5, 0.65 in | 2.5, 0 in |
Group header controls
Control | DataField | Size | Text (Name) | Miscellaneous | Location |
---|---|---|---|---|---|
Textbox |
SubTotal |
1, 0.2 in |
txtSubtotal1 |
OutputFormat = Currency Visible = False SummaryType = SubTotal SummaryGroup = GroupHeader1 |
5, 0 in |
RichText | 6.5, 2.1 in | AutoReplaceFields = True | 0, 0 in | ||
Label | 1, 0.198 in | Order ID | Bold | 0.875, 2.25 in | |
Label | 1, 0.198 in | Order Date | Bold | 1.875, 2.25 in | |
Label | 1, 0.198 in | Amount | Bold Alignment = Right |
4.375, 2.25 in |
Note: Event though txtSubtotal1 is hidden, setting its properties is important as it provides the value and the formatting that is displayed in the RichText control. |
Detail fields
Field | Size | Miscellaneous | Location |
---|---|---|---|
OrderID | 1, 0.2 in | 0.875, 0 in | |
OrderDate | 1, 0.2 in | OutputFormat = MM/dd/yy | 1.875, 0 in |
Subtotal | 1, 0.2 in | OutputFormat = Currency Alignment = Right |
4.375, 0 in |
Group footer controls
Control | Size | Text | Miscellaneous | Location |
---|---|---|---|---|
Label | 1.35, 0.198 in | Best regards, | Alignment = Right | 5.15, 0.15 in |
Label | 1.35, 0.198 in | Accounts Receivable | 5.15, 0.8 in |
Page footer label
Alignment | Size | Text | Location |
---|---|---|---|
Center | 6.5, 0.198 in | GrapeCity, 401 Parkplace, Suite 411, Kirkland, WA 98033 | 0, 0 in |
Paste into the RichText control |
Copy Code |
---|---|
Dear [!ContactName], Thank you for your business. Below is a list of your orders for the past year with a total of [!SubTotal]. Please take this opportunity to review each order and total for accuracy. Call us at 1-800-DNT-CALL with any questions or concerns. |
To write the code in Visual Basic
The following example shows what the code for the method looks like.
Visual Basic.NET code. Paste JUST ABOVE the FetchData event. |
Copy Code |
---|---|
Dim region As String |
Visual Basic.NET code. Paste INSIDE the FetchData event. |
Copy Code |
---|---|
'If there is no region for the customer, display nothing If Fields("Region").Value Is System.DBNull.Value Then region = "" Else 'If there is a region, add a comma and a space region = ", " + Fields("Region").Value End If |
To write the code in C#
The following example shows what the code for the method looks like.
C# code. Paste JUST ABOVE the FetchData event. |
Copy Code |
---|---|
string region; |
C# code. Paste INSIDE the FetchData event. |
Copy Code |
---|---|
if(Fields["Region"].Value is System.DBNull) region = ""; else region = ", " + Fields["Region"].Value.ToString(); |
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the Group Header Format event. |
Copy Code |
---|---|
'Use the current date in the letter Me.RichTextBox1.ReplaceField("Date", System.DateTime.Today.Date.ToShortDateString()) 'Use the value returned by the FetchData event Me.RichTextBox1.ReplaceField("Region", region) |
To write the code in C#
C# code. Paste INSIDE the Group Header Format event. |
Copy Code |
---|---|
//Use the current date in the letter this.RichTextBox1.ReplaceField("Date", System.DateTime.Today.Date.ToShortDateString()); //Use the value returned by the FetchData event this.RichTextBox1.ReplaceField("Region", region); |
Note: We use the BeforePrint event instead of the Format event to get the final value of the subtotal field just prior to printing. For more information on section event usage, see the Section Events topic. |
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the Group Header BeforePrint event. |
Copy Code |
---|---|
'Use the value from the hidden group subtotal field Me.RichTextBox1.ReplaceField("SubTotal", Me.txtSubtotal1.Text) |
To write the code in C#
C# code. Paste INSIDE the Group Header BeforePrint event. |
Copy Code |
---|---|
//Use the value from the hidden group subtotal field this.RichTextBox1.ReplaceField("SubTotal", this.txtSubtotal1.Text); |