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:

.AddBookmark("New Bookmark", 8)

Detail_Format(object sender, System.EventArgs eArgs) (int i = 0; i<Document.Pages.Count;i++)

this.Document.Pages[i].AddBookmark("New Bookmark", 25);

MrsdE Fjtters:5tE Ma na Ande rs Ob ara St'. 57 Berlin,

Germany 12233 Dear Maiip Anders.

Thankyo j fcycj' belowf 5-1 of yo^i drda'îfd' Lha aaïiysjj with a id La : or 3 4-, 2 7 3.33.

Fesse ta se this p p o p rtj n .ty tri re view e a ch prdar and tpta:fpr appjnapy. Ca jb. aL 1-332-DI4T-C4.LL with anyqjeït.pnï or ponpems..

Adding an ActiveReport to the 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 rptLetter.

4. Click Open.

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

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 appropriate access path.

5. Click OK to continue.

6. In the Query field, type "SELECT Customers.CustomerlD, Customers.CompanyName, Customers.ContactName, Customers.Address,

Customers.City, Customers.Region, Customers.Country, Customers.PostalCode, Orders.OrderlD, Orders.OrderDate, [Order Subtotals].Subtotal

FROM Customers INNER JOIN ([Order Subtotals] INNER JOIN Orders ON [Order Subtotals].OrderID = Orders.OrderlD) ON Customers.CustomerID = Orders.CustomerID".

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

Adding controls to the report to contain data To add controls to the report

1. Add a GroupHeader/Footer section to rptLetter.

Ip&ÇÏ Monday, Septerinser25. 1395 13952 Mpnday, Apr-15,1333

3314.53 3+7123 S3" 3.33 334 5.33 3333.53 3333.33

13632 F'day. Novambsr 33.1935 13335 Thursday, Fab^jary 15. 13&5 1 1-31 1 Thursday. May 39, 1395

Ip&ÇÏ Monday, Septerinser25. 1395 13952 Mpnday, Apr-15,1333

3314.53 3+7123 S3" 3.33 334 5.33 3333.53 3333.33

13632 F'day. Novambsr 33.1935 13335 Thursday, Fab^jary 15. 13&5 1 1-31 1 Thursday. May 39, 1395

13 7 32 fi-'onday, IMpvernbsr 13, 1935

2. Make the following changes to the group header:

o Change the name to ghCustomerlD o Change the DataField property to CustomerlD o Change the ColumnLayout property to False o Change KeepTogether property to True

3. Make the following changes to the group footer:

o Change the name to gfCustomerlD o Change the ColumnLayout property to False o Change KeepTogether property to True

4. Change the KeepTogether property for the group header and group footer to True. Change the NewPage property of the group footer to After.

5. Add the following controls to the PageHeader section:

Control Name Text/Caption Location

Picture imgLogo (Empty string) 4, 0

Label lblNorthWind NorthWind 5, 0.0625

Label lblTraders Traders 5, 0.4375

Add the following controls to the GroupHeader section:

Control DataField RichText (Empty string)

Textbox Subtotal

Label Label Label

(Empty string) (Empty string) (Empty string)

Name Text/Caption Misc Details rtf (Empty string) (Empty string)

Visible = False

OutputFormat = Currency txtTotalOrders (Empty string) SummaryType = SubTotal

SummaryGroup = ghCustomerID

lblOrderID Order ID lblOrderDate Order Date lblAmount Amount

(Empty string) (Empty string) (Empty string)

Location

2.25

2.25

2.25

7. Add the following controls to the Detail section:

Control DataField Name Text/Caption Misc Details Location

Textbox OrderlD txtOrderID Order ID Align: right 0.8125, 0

Textbox OrderDate txtOrderDate Order Date OutputFormat = Long Date 1.875, 0

OutputFormat = Currency

Textbox Subtotal txtSubtotal Subtotal 4.3125, 0

Align: right

Add the following controls to the GroupFooter section:

Control Name Text/Caption Location

Label lblYours Yours, 0.1875, 0.125

Label lblNTAP Accounts Receivable 0.1875, 0.4375

9. Add the following controls to the PageFooter section:

Control Name Text/Caption Location

Label lblNWAddress NorthWind Traders, One Portals Way, Twin Points WA 0, 0.0625

98156

Adding fields to the RichText control To add fields to the RichText control

1. Double-click inside the RichText control box to select it.

2. Right-click inside the box and choose Insert Field.

3. Insert the following fields in the following order:

o Date o CompanyName o ContactName o AddressLine o City o Region o Country o PostalCode o ContactName o TotalOrders

4. Add the following text to the RichText control box: "Dear [IContactName], Thank you for your business, below is a list of your orders for the past year with a total of [ITotalOrders]. Please take this opportunity to review each order and total for accuracy. Call us at 1-800-DNT-CALL with any questions or concerns.

5. Your RichText control should be arranged like the following.

[ICoinpanyNarnej [ICcntactName] [lAddressLinel [¡City], [¡Region] [¡Country] [IPostalCodej Dear [IContsctNsme],

Thank you for your business, below is a iist of your orders for the past year with a total of [! TotalOrders], Piease take this opportunity to review eaoh order and total for accuracy. Call us at 1-8UG-QNT-CALL. with any questions or concerns.

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_addressLine 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.Active ActiveReport.FetchEventArgs) Handles MyBase.FetchData m_companyName = Fields("CompanyName").Value m_contactName = Fields("ContactName").Value m_addressLine = 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

End Sub

[C#]

stri

ng

m

companyName

stri

ng

m

contactName

stri

ng

m

addressLine

stri

ng

m

city;

stri

ng

m

region;

stri

ng

m

country;

string m_postalCode;

private void rptLetter_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport.Fe {

m_companyName = Fields["CompanyName"].Value.ToString(); m_contactName = Fields["ContactName"].Value.ToString(); m_addressLine = 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

ghCustomerID_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handl rtf.ReplaceField rtf.ReplaceField rtf.ReplaceField rtf.ReplaceField rtf.ReplaceField rtf.ReplaceField rtf.ReplaceField rtf.ReplaceField

'CompanyName", m_companyName) 'ContactName", m_contactName) 'AddressLine", m_addressLine) 'City", m_city) 'Region", m_region) 'Country", m_country) ' PostalCode", m_postalCode) 'Date", System.DateTime.Today.Date)

End Sub

private void ghCustomerID_Format(object sender, System.EventArgs eArgs) {

th

s

rtf

th

s

rtf

th

s

rtf

th

s

rtf

th

s

rtf

th

s

rtf

th

s

rtf

this

rtf

.Repla .Repla .Repla .Repla .Repla .Repla .Repla .Repla ceField ceField ceField ceField ceField ceField ceField ceField

'CompanyName", m_companyName) 'ContactName", m_contactName) 'AddressLine", m_addressLine) 'City", m_city); 'Region", m_region); 'Country", m_country); 'PostalCode", m_postalCode);

'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) ghCustomerlD.BeforePrint Me.rtf.ReplaceField("TotalOrders", Me.txtTotalOrders.Text)

private void ghCustomerID_BeforePrint(object sender, System.EventArgs eArgs) {

this.rtf.ReplaceField("TotalOrders",this.txtTotalOrders.Text);

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.

Was this article helpful?

0 0

Post a comment