Changing Text Objects at Run Time


With older integration approaches, changing textual information on the report, such as a company name or a description of the report s selection criteria, must be done with a formula. Typically, the formula simply contains a text literal (a text string surrounded by quotation marks or apostrophes ) that is then changed at run time. While this will still work with the RDC, the RDC permits another approach that can potentially give you much more flexibility.

Remember that when you add any object to the report (a field, formula, text object, or other object), the object can be manipulated by VB. It has properties that can be set on the Property Pages dialog box at design time, as well as being modified at run time. If you add a text object to the report, a TextObject object becomes available from within the overall Report object. The TextObject object exposes many properties (you can see most of them in the Properties dialog box) that can be set at run time.

You can access the actual contents of the text object with a property and a method. The Text property can be read during code execution to see what the text object contains. To change the contents of a text object, you need to use the TextObject SetText method at run time.

In the Xtreme Orders sample application, a text object has been placed in the report header and given the name SubHeading. The following code indicates how to set the contents of the text object to indicate what choices the user has made on the Print Xtreme Orders form. This code assumes that the strSubHeading string variable has been declared elsewhere in the application. You ll also notice references to the text object and combo box controls on the form.

 'Set SubHeading text object 
strSubHeading = txtFromDate & " through " & txtToDate
strSubHeading = strSubHeading & ", By " & cboGroupBy
If txtTaxRate = "" Then
strSubHeading = strSubHeading & ", No Tax"
Else
strSubHeading = strSubHeading & ", Sales Tax = " & txtTaxRate & "%"
End If 'txtTaxRate = ""
Report.SubHeading.SetText (strSubHeading)
'Note: parentheses around argument to SetText method are optional

If you have used other integration methods in the past, you may want to change the general approach that you have used for custom formulas. Because you can change the contents of a text object from your VB code, you can essentially create VB formulas in your report by using the RDC. Each section of the report will fire a Format event when it is processed at report run time. You can use the SetText method inside this Format event to change the contents of text objects for each report record (in the details section Format event) or for group or report headers or footers (in their respective Format events). More information on the Format event and conditional section formatting is provided later in this chapter.

The RDC limits some of this ease of individual object control to only reports created with the RDC s internal ActiveX designer. If you use an external .RPT file with the RDC Library, you must sometimes navigate much deeper down the object hierarchy to perform this type of customization. For example, if you have just set up Application and Report objects that point to an external .RPT file (there is no ActiveX designer in your project), and you wish to set the contents of a text object, you ll need to execute code similar to this:

 Report.Sections("PHb").ReportObjects(1).SetText "Order #" 

This code will set the contents of the first text object in Report Header b to Order # . This is accomplished by navigating into the Sections collection (one of the RDC collections that allows string indexes to be used, in addition to numbers ). Within the Page Header b section, the first member of the ReportObjects collection (the text object) is being changed by executing the SetText method.

Tip  

Version 10 of the RDC also allows you to manipulate embedded fields inside text objects. Using the TextObject object s FieldElements collection, you can add, remove, or query for embedded fields inside text objects.




Crystal Reports 10
Crystal Reports 10: The Complete Reference
ISBN: B005DI80VA
EAN: N/A
Year: 2004
Pages: 223
Authors: George Peck

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net