Sunday 23 April 2017

Control Structures (Crystal Syntax) In Crystal Report : If Expressions (Crystal Syntax) in Crystal Report

Formulas without control structures execute each expression in the formula exactly once when the formula is evaluated. The expressions are executed in a sequential fashion, from the first expression in the formula to the last. Control structures enable you to vary this rigid sequence. Depending upon which control structure you choose, you can skip over some of the expressions or repeatedly evaluate some expressions depending on certain conditions. Control structures are the primary means of expressing business logic and typical report formulas make extensive use of them.
If Expressions (Crystal Syntax)
The If expression is one of the most useful control structures. It allows you to evaluate an expression if a condition is true and evaluate a different expression otherwise.
Note   When formatting with conditional formulas, always include the Else keyword; otherwise, values that don't meet the If condition may not retain their original format. To prevent this, use the DefaultAttribute function (If...Else DefaultAttribute).
Example
A company plans to pay a bonus of 4 percent to its employees except for those who work in Sales who will receive 6 percent. The following formula using an If expression would accomplish this:
//If example 1
If {Employee.Dept} = "Sales" Then
   {Employee.Salary} * 0.06
Else
   {Employee.Salary} * 0.04
In this example, if the condition {Employee.Dept} = "Sales" evaluates as true, then the
{Employee.Salary} * 0.06
expression is processed. Otherwise the expression following the Else, namely the
{Employee.Salary} * 0.04
is processed.
Suppose another company wants to give employees a 4% bonus, but with a minimum bonus of $1,000. Notice that the Else clause is not included; it is optional, and not needed in this case.
//If example 2
Local CurrencyVar bonus := {Employee.Salary} * 0.04;
If bonus < 1000 Then
   bonus := 1000;
//The final expression is just the variable 'bonus'.
//This returns the value of the variable and is the
//result of the formula
bonus
Another way of accomplishing example 2 is to use an Else clause:
//If example 3
Local CurrencyVar bonus := {Employee.Salary} * 0.04;
If bonus < 1000 Then
   1000
Else
   bonus
Now suppose that the previous company also wants a maximum bonus of $5,000. You now need to use an Else If clause. The following example has only one Else If clause, but you can add as many as you need.
Note   There is a maximum of one Else clause per If expression.
The Else clause is executed if none of the If or Else If conditions are true.
//If example 4
Local CurrencyVar bonus := {Employee.Salary} * 0.04;
If bonus < 1000 Then
   1000
Else If bonus > 5000 Then
   5000
Else
   bonus;
Example
Suppose that a company wants to compute an estimate of the amount of tax an employee needs to pay and write a suitable message. Income below $8,000 is not taxed, income between $8,000 to $20,000 is taxed at 20%, income between $20,000 to $35,000 is taxed at 29%, and income above $35,000 is taxed at 40%.
//If example 5
Local CurrencyVar tax := 0;
Local CurrencyVar income := {Employee.Salary};
Local StringVar message := "";
If income < 8000 Then
(
   message := "no";
   tax := 0
)
Else If income >= 8000 And income < 20000 Then
(
   message := "lowest";
   tax := (income - 8000)*0.20
)
Else If income >= 20000 And income < 35000 Then
(
   message := "middle";
   tax := (20000 - 8000)*0.20 + (income - 20000)*0.29
)
Else
(
   message := "highest";
   tax := (20000 - 8000)*0.20 + (35000 - 20000)*0.29 +
          (income - 35000)*0.40
);
//Use 2 decimal places and the comma as a
//thousands separator
Local StringVar taxStr := CStr (tax, 2, ",");
"You are in the " & message & " tax bracket. " &
"Your estimated tax is " & taxStr & "."

Formula Size Restrictions in Crystal Report

For reference purposes, here are the size restrictions of the formula language:
  • The maximum length of a String constant, a String value held by a String variable, a String value returned by a function or a String element of a String array is 65,534 characters.
  • The maximum size of an array is 1000 elements.
  • The maximum number of arguments to a function is 1000. (This applies to functions that can have an indefinite number of arguments such as Choose).
  • The maximum number of loop condition evaluations per evaluation of a formula is 100,000.
  • Date-time functions modeled on Visual Basic accept dates from year 100 to year 9999. Traditional Crystal Reports functions accept dates from year 1 to year 9999.

Preventing Infinite Loops (Basic Syntax) in Crystal Report

There is a safety mechanism to prevent report processing from hanging due to an infinite loop. Any one evaluation of a formula can have at most 100,000 loop condition evaluations per formula evaluation. For example:
Dim i
i = 1
Do While i <= 200000
   If i > {movie.STARS} Then Exit Do
   i = i + 1
Loop
formula = 20
If {movie.STARS} is greater than 100,000 then the loop condition (i <= 200000) will be evaluated more than the maximum number of times and an error message is displayed. Otherwise the loop is OK.
Note   The safety mechanism applies on a per formula base, not for each individual loop. For example:
Dim i
i = 1
For i = 1 To 40000
   formula = Sin (i)
Next i
Do While i <= 70000
   i = i + 1
Loop
The above formula also triggers the safety mechanism since the 100,000 refers to the total number of loop condition evaluations in the formula and this formula will have 40001 + 70001 such evaluations.

Changing the Data Source Location Of any Existing Report in Crystal Report

Use the Set Location command to indicate the new name or location of tables that are active in a report. This is especially useful if someone sends you a report that uses a database that has a different location on your system, or if you have changed the directory or disk location of a database. In addition, the Set Location command automatically converts your database driver to the data source you have chosen. For example, you can automatically convert a direct access data source to an ODBC data source using the Set Location command.
The procedure is the same whether you are replacing all tables from a particular data source with tables of the same name from another data source, or replacing an individual table with another table.
To change the data source location
  1. Right-click in the embedded Crystal Reports Designer, point to Database, and click Set Datasource Location.
  2. From the Current Data Source list, choose the data source table from which you want to change.
  3. In the Replace with list, browse for the new data source.
  4. Select the table you want to change to.
  5. Click Update.
  6. Click Close after you have changed all tables you want to change; otherwise repeat these steps for any unmodified tables or data sources.

Use Features of Underline Following Section From Section Expert

Underlaying objects placed in report section appear next to or underneath objects placed in the following report section.
Note   The underlay feature works by report section. All objects in that section will underlay the following section.
You can use the underlay feature to format reports for pre-printed forms, or to insert a chart or an employee picture to print beside the details pertaining to the chart or employee.
If you print on pre-printed forms, you will be able to:
  • scan a form
  • place it in the report as a bitmap
  • use the underlay feature to line up the bitmap and report, as well as move objects anywhere you want them to appear
  • eliminate the need to print the forms separately by printing the report and the form as a single unit
To underlay report objects
  1. Add the objects you want to underlay to a report section.
  2. Right-click the report section and select Section Expert.
  3. Select the Underlay Following Sections check box.
  4. Click OK.
  5. Resize the object vertically if you want it to underlay more sections.
The area in which the object underlays depends on the following conditions:
    • size of the object
    • section in which the object was originally placed
    • position of the object in the section

Implementing Report Part Drill Down

The Report Part Drilldown option in the Hyperlink tab of the Format Editor lets you define a hyperlink so that the Report Part Viewer can emulate the drill-down functionality of Crystal Reports. The Report Part Viewer displays only destination objects; therefore, to make drill down work, you need to define a navigation path from a home object to one or more destination objects. When you have multiple destination objects, they must all reside in the same report section.
The Report Part Drilldown option does not affect the DHTML page viewers since the option emulates the default Crystal Reports behavior for drill down (which the page viewers already support). Page viewers, however, do not limit which objects are displayed — they always show all report objects.

Defining the Home Object

Because the Report Part Viewer shows only destination objects, you must define Initial Report Part Settings (that is, a default home object) for a report before your Report Part Drilldown hyperlinks can work. A report's Initial Report Part Settings define the object that appears first in the Report Part Viewer. Think of this object as the place you'll begin your path of drill-down hyperlinks from.
To define Initial Report Part Settings
  1. Open the report whose default home object you want to define.
  2. Click the object you want to set as the default home object and copy its name listed in the Name field of the Properties window (for example: Text1, Field1, Graph1, and so on).
  3. Right-click in the embedded Crystal Reports Designer, point to Report, and select Report Options.
  4. In the Initial Report Part Settings area of the Report Options dialog box, click the Paste Linkbutton.
The name and data context of the report object you selected as your home object are pasted into the appropriate fields.
Include another object from the same section of the report by entering a semi-colon (;) and typing the name of the object.
  1. Click OK.
To create a Report Part Drilldown hyperlink
  1. Open a report and right-click the intended destination object and select Format from the shortcut list.
  2. In the Format Editor, click the Hyperlink tab.
  3. In the DHTML Viewer Only area, select Report Part Drilldown.
The Hyperlink Information area changes to show the appropriate fields for this type of hyperlink.
The Available Fields area shows only the sections and report objects you can select for drill down. In general, these objects include field objects, charts, maps, bitmaps, cross-tabs, and text objects contained in the next section down (that is, for example, objects in group two when the object you selected is in group one).
Note   The Available Fields area does not show suppressed report objects.
  1. In the Available Fields area, select the section or report object(s) you want to use as your drill down destination.
Tip   Use the Properties Window to quickly identify the default names assigned to each of your report objects.
  1. Use the arrow buttons to move the selected section or object(s) to the Fields to Display area.
For ease of identification, the program creates a section node in the Fields to Display area. This node contains the objects that you selected in the Available Fields area.
  1. If you want to add another object to the Fields to Display area, select it in the Available Fields list and drag it into position.
Tip   The position an object has in the Fields to Display area determines how it appears in the Report Part Viewer; top to bottom in the Fields to Display area equates to left to right in the viewer. Use the arrows above the Fields to Display area to change the order of objects.
Note   The Fields to Display area can contain only one section at a time. If you try to add a second section, it replaces any existing section in the Fields to Display area.
  1. Click OK.
You have established a hyperlink from your report's home object to a destination object or objects. In the Report Part Viewer, you will see the home object first, and when you click it for drill down, you will see the destination object(s).

Chart Basic In Crystal Report

Crystal Reports enables you to present summarized data in colorful, easy
-to read charts. This section demonstrates how to create charts and how to use them in reports to make report data more meaningful and easier to understand. You can choose from a number of chart layouts and types, as well as drill down, to see the details behind the graphical summaries and format chart objects.

Charting concepts

Charting overview

Crystal Reports enables you to include sophisticated, colorful charts in your reports. You can use charts any time you want to improve the usefulness of a report.
For example, if you have a sales report grouped by Region with a subtotal of Last Year's Sales for each region, you can quickly create a chart that will display Sales per Region.

You can chart on the following:
  • Summary and subtotal fields.
  • Detail, formula, and Running Total fields.
  • Cross-Tab summaries.
  • OLAP data.
You will typically chart on summary and subtotal information at the group level. However, depending on the type of data you are working with, you can create an Advanced, Cross-Tab, or OLAP grid chart for your report.

Chart layouts

The Chart Expert provides four layouts that correspond to certain sets of data.
You can create charts with any of the following layouts, and be depending on the data you are using, you can change the chart from one layout to another.

Advanced
Use the Advanced layout when you have multiple chart values or when you do not have any group or summary fields in the report.
The Advanced chart layout supports one or two condition fields: with these condition fields, you can create a 2-D, 3-D, or pie chart. Other specific functions with the Advanced layout include:
  • Values can be grouped in ascending, descending, or specified order, as well as by Top N or Sort totals.
  • Values can be plotted for each record.
  • Values can be plotted as a grand total for all records.
  • Charts can be based on formula and Running Total fields.

Group
The Group layout is a simplified layout in which you show a summary on change of field for topics such as Country.
Note:    In order to create a chart using the Group layout, you must have at least one group and at least one summary field for that group.

Cross-Tab
Use the Cross-Tab layout to chart on a Cross-Tab object. A Cross-Tab chart uses the fields in the cross-tab for its condition and summary fields.

OLAP
Use the OLAP layout to chart on an OLAP grid. An OLAP chart uses the fields in the OLAP grid for its condition and summary fields.

Chart types

Different sets of data are particularly suited to a certain chart type. The following is an overview of the main chart types and their most common uses.

Bar
Most bar charts (also known as a column chart) display or compare several sets of data. Two useful bar charts are the Side-by-Side bar chart and the Stacked bar chart.
  • Side-by-Side bar chart
    A Side-by-Side bar chart displays data as a series of vertical bars. This type of chart is best suited for showing data for several sets over a period of time (for example, last year's sales figures for AZ, CA, OR, and WA).
  • Stacked bar chart
    A Stacked bar chart displays data as a series of vertical bars. This type of chart is best suited for representing three series of data, each series represented by a color stacked in a single bar (for example, sales for 1997, 1998, and 1999).

Line
A line chart displays data as a series of points connected by a line. This type of chart is best suited for showing data for a large number of groups (for example, total sales over the past several years).

Area
An area chart displays data as areas filled with color or patterns. This type of chart is best suited for showing data for a limited number of groups (for example, percentage of total sales for AZ, CA, OR, and WA).

Pie
A pie chart displays data as a pie, split and filled with color or patterns. Pie charts are typically used for one group of data (for example, the percentage of sales for the entire inventory); however, you have the option to choose multiple pie charts for multiple groups of data.

Doughnut
A doughnut chart is similar to a pie chart, displaying data as sections of a circle or doughnut. If, for example, you charted sales by region on a particular report, you would see the total number of sales (the figure) in the center of the doughnut and the regions as colored sections of the doughnut. As with the pie chart, you have the option to choose multiple doughnut charts for multiple groups of data.

3-D Riser
A 3-D Riser chart displays data in a series of 3-dimensional objects, lined up side-by-side, in a 3-dimensional plane. The 3-D Riser chart shows the extremes in your report data. For example, the differences between sales by customer by country are visually dynamic when presented in this chart.

3-D Surface
3-D Surface charts present a topographic view of multiple sets of data. If, for example, you need a chart to show the number of sales by customer by country, in a visually dynamic and relational format, you might consider using the 3-D Surface chart.

XY Scatter
An XY Scatter chart is a collective of plotted points that represent specific data in a pool of information. The XY Scatter chart allows the user to consider a larger scope of data for the purpose of determining trends. For example, if you input customer information, including sales, products, countries, months, and years, you would have a collective of plotted points that represents the pool of customer information. Viewing all of this data on an XY Scatter chart would allow you to speculate as to why certain products were selling better than others or why certain regions were purchasing more than others.

Radar
A radar chart positions group data, such as countries or customers, at the perimeter of the radar. The radar chart then places numeric values, increasing in value, from the center of the radar to the perimeter. In this way, the user can determine, at a glance, how specific group data relates to the whole of the group data.

Bubble
A bubble chart (an extension of the XY Scatter chart type) displays data as a series of bubbles, where the size of the bubble is proportional to the amount of data. A bubble chart would be very effective with the number of products sold in a certain region; the larger the bubble, the greater the number of products sold in that region.

Stock
A stock chart presents high and low values for data. It is useful for monitoring financial or sales activities.
Note:    Crystal Reports offers two possible formats for stock charts: High-Low and High-Low-Open-Close. Each of these types requires a series of values in the order specified in its name.

Numeric Axis
A numeric axis chart is a bar, line, or area chart that uses a numeric field or a date/time field as its "On change of" field. Numeric axis charts provide a way of scaling your X-axis values, thus creating a true numeric X-axis or a true date/time X-axis.

Gauge
A gauge chart presents values graphically as points on a gauge. Gauge charts, like pie charts, are typically used for one group of data (for example, the percentage of sales for the entire inventory).

Gantt
A Gantt chart is a horizontal bar chart often used to provide a graphical illustration of a schedule. The horizontal axis shows a time span, while the vertical axis shows a series of tasks or events. Horizontal bars on the chart represent event sequences and time spans for each item on the vertical axis. You should use only date fields when creating a Gantt chart. The field you choose for the data axis should be set to "For Each Record," and the start and end date fields should be added to the "Show value(s)" area of the Chart Expert's Data tab.

Funnel
Funnel charts are often used to represent stages in a sales process. For example, the amount of potential revenue shown for each stage. This type of chart can also be useful in identifying potential problem areas in an organization's sales processes. A funnel chart is similar to a stacked bar chart in that it represents 100% of the summary values for the groups included in the chart.

Where to place a chart

The placement of a chart determines which data is displayed and where it is printed. For example, if you place a chart in the Report Header section, the chart includes data for the entire report. If you place it in a Group Header or Group Footer section, it displays group specific data.

Control Structures (Crystal Syntax) In Crystal Report : If Expressions (Crystal Syntax) in Crystal Report

Formulas without control structures execute each expression in the formula exactly once when the formula is evaluated. The expressions are...