An Introduction to the RDC Object Model


While the Report object has many properties of its own (such as RecordSelectionFormula), it contains many objects and collections of objects that fall below it. One available Report object collection is FormulaFields, which contains a set of FormulaFieldDefinition objects, one for each formula defined in the report. Each of these objects contains all the properties for that particular formula, such as the formula name , the text of the formula, the data type that it returns, and other properties.

When you re dealing with a collection of objects, you need to identify which member of the collection you wish to manipulate, by using an index pointer to the appropriate member. In the RDC, indexes are one-based. That is, the first member is numbered 1, the second is numbered 2, and so on. This is in contrast to the ActiveX control and other VB object models, which often expose zero-based indexes. The RDC typically numbers the collection members in the order in which they were added to the report.

For example, to print the formula text from the first formula in the Xtreme Orders sample application, you can type the following code into the Immediate window while the program is in break mode:

 ? Report.FormulaFields(1).Text 

Here, the object hierarchy is being navigated to reveal the Text property of the first member of the FormulaFieldDefinitions collection (referred to simply as FormulaFields) of the Report object. Since the Text property is a read/write property, you not only can retrieve what the formula already contains, but also replace it by setting the Text property to a string value, as in the following:

 Report.FormulaFields(1).Text = "{Orders.Order Amount} * 1.1" 

Because the RDC is a tightly integrated COM component, you will benefit from automatic code completion (Microsoft refers to it as IntelliSense ) when you use the RDC with Visual Basic. When you begin to enter code into the VB code window for the Report object, the properties, methods , and built-in constant values will become available to you from automatically appearing drop-down lists. If you ve used Visual Basic 5 or 6, you re probably familiar with this feature. You ll enjoy the benefit it provides with the RDC.

click to expand
start sidebar
Manipulating Collection Indexes by Name Instead of Number

The RDC will allow you to use string indexes to refer to members of only certain collections, but not all of them. For example, the RDC requires you to set the contents of a formula with

 Report.FormulaFields(1).Text = "{Orders.Order Amount} * 1.1" 

while older integration methods, or Visual Studio .NET might allow

 Report.Formulas("Order + Tax").Text = "{Orders.Order Amount} * 1.1" 

You sometimes need to do some experimenting to find the actual member of a collection that you wish to manipulate.

Wouldn't it be nice if, instead of using the numeric index to set the value of a formula, you could use code like this:

 If SetFormula("Order + Tax","{Orders.Order Amount} * 1.1") Then 
MsgBox "Formula Changed"
Else
MsgBox "Incorrect Formula Name Supplied"
End If

Examine the following function code:

 Public Function SetFormula(FormulaName As String, _ 
FormulaText As String) As Boolean
Dim intCounter As Integer
For intCounter = 1 To Report.FormulaFields.Count
If Report.FormulaFields(intCounter).FormulaFieldName = FormulaName Then
Report.FormulaFields(intCounter).Text = FormulaText
SetFormula = True
Exit Function
End If 'Report.FormulaFields(intCounter).Name = FormulaName
Next intCounter
SetFormula = False
End Function

If you examine this code closely, you'll see that the FormulaFields collection can be cycled through using a For loop, looking for the formula that has the same name as the passed FormulaName argument. Once it's found, the value of the passed FormulaText argument is used to set the value of the formula. If a formula with the same name isn't found, the subroutine returns a false value that can be tested for.

If you have a large number of formulas, parameter fields, SQL expressions, or other objects that may be easier to manipulate by name rather than by index number, use this type of logic to make your coding life easier!

Also, another method exists in various RDC collections (of particular interest are the FormulaFieldDefinitions and ParameterFields collections) that allow you to retrieve an object by name rather than index. Examine the following code for an example of the GetItemByName method:

 Dim FormulaField As CRAXDRT.FormulaFieldDefinition 
Set FormulaField = Report.FormulaFields.GetItemByName("Order + Tax")

FormulaField.Text = "{Orders.Order Amount} * 1.1"

In this example, a new object variable is declared to hold a formula. It is then set to an existing formula in the report by calling the FormulaFields collection's GetItemByName method, using the actual name of the formula as an argument. The method returns an actual formula field object, which can then be manipulated by reading or setting properties.

Note that the formula or parameter field name you supply as the GetItemByName argument cannot contain the @ sign, ? sign, or curly braces ”just the formula field or parameter field name should be supplied, without any extraneous punctuation. If you supply an invalid name, the RDC will throw a trappable error to VB.

end sidebar
 



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