Professional.Crystal.Reports.for.Visual.Studio..NET [Electronic resources]

David McAmis

نسخه متنی -صفحه : 115/ 61
نمايش فراداده

Working with the Formula Editor

There are two basic types of formulas. First, there are formula fields that you can insert in your report (usually enclosed in braces and prefixed by the @ symbol, for example, {@SalesTax}). Second, there are formulas that appear "behind the scenes", like those for record selection or conditional formatting. From these two distinct types, you can create thousands of different formulas; but regardless of what you're working with, formulas are all created, debugged, and edited using the Crystal Reports formula editor.

Controlling the Editor's Appearance

The Crystal Reports formula editor has undergone a number of changes over the past few releases to move from a simple textbox to something that resembles a real code editor, including a customizable interface, color coding, and search and replace features.

To get things underway, the formula editor can be opened by either creating a new formula, or editing an existing formula. In this example, we're going to look at the formula editor by editing a formula that appears in a report (operators.rpt) that's included with the sample files for this chapter.

Open the report in the Report Designer and expand the Formula Fields section of the Field Explorer shown below.

Locate the Sales Tax formula, right-click directly on it, and select Edit. This will open the Crystal Reports formula editor, the appearance of which is controlled by a set of default values.

These values can be set in the Report Designer by right-clicking on the report and selecting Designer | Default Settings | Editors. You can set the properties for comments, keywords, text, and selected text using this dialog, including changes to the font size and color. Using these properties, the formula editor can be modified to your preferences, for example, highlighting comments in a bright color, or keywords in bold.

There's also a Reset All button that you can use if you'd like to reset the editor's settings to their original defaults.

Controlling the Syntax Type

When working with the Crystal Reports formula language, there are two different types of syntax available to you: Basic syntax and Crystal syntax. We'll look at each of these in more detail later in the chapter.

Which syntax you are working with is controlled by the drop-down list that appears in the upper right-hand corner, as shown in the previous screenshot. Each type of syntax has its own operators and functions that may (or may not!) overlap.

The code for the examples is in the Chapter07 folder:

BasicSyntax_Basic - A viewing application that contains the reports discussed in this chapter that use Basic syntax.

CrystalSyntax_Basic - An application that contains the reports discussed in this chapter that use Crystal syntax.

Checking for Syntax Errors

To check your formula for syntax errors, there is a Check icon (labeled X+2) that appears on the Formula Editor tool bar. This performs a syntax check on the formula in the window, but it doesn't guarantee that your formula will run, or produce the desired result. It just checks to make sure that you've spelled everything correctly, and that your code is well formed.

With the operators report open in the form designer, right-click on the Total formula under Formula Fields in the Field Explorer, to open the Formula Editor. Click on the Check icon and you will receive the following message:

Click on OK, and go back to the Formula Editor. Enter some random characters after the formula, and click on the Check icon again. This time an error dialog is displayed:

In this case, the checker has correctly identified an error where some characters have been inserted at the end of a formula. If you do not correct this error, you will receive another warning when you attempt to save the formula:

You can select Yes to leave the formula without correcting the error, but doing so may cause further errors when your report is run.