Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

20.12 Adding a Special Field

Adding a Special Field to the Report

Crystal Reports has special fields that give you more in-depth information about a report. Some of these include Page Number, Page N of M, Print Time, etc. The complete list is found in the Field Explorer window in the Special Fields node.

Adding a special field to a report is almost identical to adding a database field. The difference is that the special field is an internal field and not associated with an external data source. Because of this, rather than set the DataSource property to the name of a table’s field, you assign it a pre-defined constant. Crystal Reports will recognize that it is special field and palce it on the report.

Each pre-defined constant name is almost identical to its related special field name. The pre-defined constant has the spaces removed. In a few unique cases, the name is an abbreviation of the special field name. To make it easy, the name of every special field is listed in Table 20-3. The left column is the special field name and the right column is the value you assign to the DataSource property.

Table 20-1. Special Fields and their pre-defined constants.

Special Field DataSource Name
Data Date DataDate
Data Time DataTime
File Author FileAuthor
File Path and Name FileName
Group Number GroupNumber
Group Selection Formula GroupSelection
Modification Date ModificationDate
Modification Time ModificationTime
Page N of M PageNofM
Print Date PrintDate
Print Time PrintTime
Record Number RecordNumber
Record Selection Formula RecordSelection
Report Title ReportTitle
Total Page Count TotalPageCount
Content Locale ContentLocale
Data Time Zone DataTimeZone
Print Time Zone PrintTimeZone
Horizontal Page Number HPageNUmber

The following code creates the report object which holds a special field. To use it in your program, you can take the code in Listing 20-9 and replace the call to AddDatabaseField() with a call to AddSpecialField(). When calling the AddSpecialField() method, the first argument is the special field name that designates which field you want to put on the report. Use the name from column two in Table 20-1.

Listing 20-10. Add a Special Field to the report.
[VB.NET]
Public Shared Sub AddSpecialField(ByVal Name As String, ByVal Top As Integer, ByVal Left As Integer, ByVal Height As Integer, ByVal Width As Integer, _
ByVal Section As CrystalDecisions.ReportAppServer.ReportDefModel.Section, ByVal FontColor As CrystalDecisions.ReportAppServer.ReportDefModel.FontColor, _
ByVal rcd As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument)
Dim myFieldObject As CrystalDecisions.ReportAppServer.ReportDefModel.ISCRFieldObject
myFieldObject = New CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject()
myFieldObject.DataSource = Name
myFieldObject.Left = Left
myFieldObject.Top = Top
myFieldObject.Width = Width
myFieldObject.Height = Height
myFieldObject.FontColor = FontColor
'Add the report object to the correct section
rcd.ReportDefController.ReportObjectController.Add(myFieldObject, Section, 0)
End Sub
[C#]
public static void AddSpecialField(string Name, int Top, int Left, int Height, int Width, CrystalDecisions.ReportAppServer.ReportDefModel.Section Section,
CrystalDecisions.ReportAppServer.ReportDefModel.FontColor FontColor,
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rcd)
{
CrystalDecisions.ReportAppServer.ReportDefModel.ISCRFieldObject myFieldObject;
myFieldObject = new CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject();
//This is the name of the special filed
myFieldObject.DataSource = Name;
myFieldObject.Left = Left;
myFieldObject.Top = Top;
myFieldObject.Width = Width;
myFieldObject.Height = Height;
myFieldObject.FontColor = FontColor;
//Add the report object to the correct section
rcd.ReportDefController.ReportObjectController.Add(myFieldObject, Section, 0);
}