ActiveReports 6 Online Help
Mail Merge with RichText
Show AllShow All
Hide AllHide All

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:

To connect the report to a data source

  1. Add a report to a Visual Studio project, naming it rptLetter.
  2. Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
  3. On the "OLE DB" tab, next to Connection String, click the Build button.
  4. In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button.
  5. Click the ellipsis (...) button to browse to the Northwind database. Click Open once you have selected the appropriate access path.
  6. Click OK to close the window and fill in the Connection String field.
  7. In the Query field, enter the following SQL query
    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
  8. Click OK to save the data source and return to the report design surface.

To add controls and format the report

  1. Right-click the design surface of the report and select Insert, then Group Header/Footer to add group header and group footer sections.
  2. In the Properties Window, make the following changes to the group header section:
    • DataField: CustomerID (This sets a new group for each customer.)
    • Height: 2.5
    • KeepTogether: True
  3. Make the following changes to the group footer section:
    • Height: 1.1
    • KeepTogether: True
    • NewPage: After (This ensures that a new page begins after each customer's letter has finished rendering.)
  4. Make the following changes to the detail section:
    • CanShrink: True
  5. Make the following changes to the page header section:
    • Height: 0.8
  6. Add the following controls to the PageHeader section and set the properties as indicated.

    Page header controls

  7. In the Report Explorer, expand the Fields node, then the Bound node. Drag the SubTotal field onto the group header section, add the following controls from the ActiveReports toolbox, and set the properties as indicated.

    Group header controls

    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.
  8. In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section and set the properties of each textbox as indicated.

    Detail fields

  9. Add the following controls to the GroupFooter section and set the properties as indicated.

    Group footer controls

  10. Add a label control to the PageFooter section and set the properties as indicated.

    Page footer label

To add fields to the RichText control

  1. Double-click inside the RichText control box and delete the default text.
  2. Right-click inside the box and choose Insert Fields.
  3. In the Insert Field dialog that appears, enter Date and click the OK button.
  4. Place the cursor in front of the text [!Date] that appears in the RichText control, and add spaces until the text is at the right edge of the control (but not overlapping to the next line).
  5. Place the cursor at the end of the text, and press the Enter key to move to the next line.
  6. Insert each of the following fields using the Insert Field dialog (see image below for arrangement of fields):
    • CompanyName
    • ContactName
    • Address
    • City
    • Region
    • Country
    • PostalCode
    • ContactName
  7. Add the following text to the RichText control box after all of the fields:
    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.
    
  8. Arrange the text and fields within the control as you would in any text editor to look like the following.

To use the FetchData event to conditionally format data

To write the code in Visual Basic

To write the code in C#

To add code to update RichText fields with the current date and conditional values

  1. Double-click in the group header section of the report to create an event-handling method for the group header's Format event.
  2. Add code to the handler to:
    • Replace the Date field in the RichText control with the current system date
    • Replace the Region field with the conditional value created in the FetchData event

To write the code in Visual Basic.NET

To write the code in C#

To add code to send the group subtotal value to the RichText field

  1. Right-click in any section of the design window of rptLetter, and click on View Code to display the code view for the report.
  2. At the top left of the code view for rptLetter, click the drop-down arrow and select GroupHeader1.
  3. 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 GroupHeader1_BeforePrint event.
    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.
  4. Add code to the handler to replace the value of the Subtotal field in the RichText control with the value of the hidden textbox in the group header.

To write the code in Visual Basic.NET

To write the code in C#

See Also

How To