Range Data Types
The Range data type is a very useful data type that doesn’t exist in .NET. It allows you to store multiple values within a single variable. In .NET, if you want to be able to store a range of values that have a definite start and end, then you have to declare two variables where each variable represents and endpoint. If you want to see whether a field falls within this range, then you have to compare the field to both variables. Crystal Reports greatly simplifies this with the Range data type. It stores a range within one variable and performs tests to see if another variable falls within that range. However, you can’t just store any values in this variable. It must be a group of values with a definite starting and ending. All values in between are included. Of course, it should go without saying that the starting point and end point must be of the same data type.
To declare a variable as a Range data type, declare it as one of the standard data types and put the Range keyword at the end. The data types that are allowed to have a related Range data type are Number, Currency, String, DateTime, Date, and Time.
Crystal syntax puts the Range keyword after the data type.
Defining a range uses a variety of different operators. These operators specify the beginning and end of the range. The most basic operator is the To operator. It is placed between the start and end values. Using the To operator means that you want the start and end values to be included as valid members of the range.
‘Demonstrate creating a range of all the days in a year.
Dim DaysInYear as Date Range
DaysInYear = #1/1/2002# To #12/31/2002#
A variation of the To operator is to use it with the underscore character. Placing the underscore on one side of the To operator states that you want all values leading up to that constant to be included, but you don’t want the constant specified to be included. The underscore can be placed on either side, or both sides.
The following example demonstrates creating a range with all the days in the month. However, we only want to do so using the first day of the months in our range. The date 2/1/2002 won’t be included in our range. Instead, the last day of the month just prior to it will be included.
Dim DaysInMonth As Date Range
DaysInMonth = #1/1/2002# To_ #2/1/2002#
When you want to find out if a field or variable is included within a specified range, use the In operator. This is useful when writing If statement and Select Case statements.
‘Demonstrate calculating a volume price discount
Select Case UnitsSold
Case In (1 To_ 1000)
Formula = Price
Case In (1000 To_ 5000)
Formula = Price * .95
Case >= 5000
Formula = Price * .90
End Select
Basic syntax has many predefined date range constants that can be used in your report. These are commonly used to filter out records that have dates that don’t fall within the specified range. Table 8-3 lists the predefined date ranges and specifies which dates are considered to be included. Many of these constants use today’s date to determine one of the end points of the range. You can tell the report to use a date other than the system date by setting the PrintDate property. This is done by right clicking on the Field Explorer, selecting Report, and then selecting Set Print Date. This is saved with the report.
Table 8-3. Predefined Date Range Constants
Name | Description |
---|---|
AllDatesToToday | Includes any date prior to, and including, today. |
AllDatesToYesterday | Includes any date prior to today. Today is not included. |
AllDatesFromToday | Start: Today. |
End: Last date in field. | AllDatesFromTomorrow |
Start: Tomorrow. | End: Last date in field. |
Aged0To30Days, Aged31To60Days, Aged61To90Days | Groups dates in 4 blocks of 30 days prior to today. Today’s date is included in the Aged0To30Days range. |
Calendar1stQtr, Calendar2ndQtr, Calendar3rdQtr, Calendar4thQtr | Groups dates in blocks of 3 months each. The first date is Jan 1 of the current year. |
Calendar1stHalf, Calendar2ndHalf | Start: Jan 1 of the current year thru June 30. |
End: July 1 of the current year thru December 31. | Last7Days |
Start: The six days prior to today. | End: Today. |
Last4WeeksToSun | Start: The first Monday of the four weeks prior to last Sunday. |
End: Last Sunday. | Note: Does not include the days after the last Sunday thru today. |
LastFullWeek | Start: The Sunday of the last full week. |
End: The Saturday of the last full week. | Note: Does not include the days after Saturday thru today. |
LastFullMonth | Start: The first day of last month. |
End: The last day of last month. | MonthToDate |
Start: The first day of this month. | End: Today. |
Next30Days, Next31To60Days, Next61To90Days, Next91To365Days | Groups dates in 4 blocks of 30 days after today. Today’s date is included in the Next30Days range. |
Over90Days | Includes all days that come before 90 days prior to today. |
WeekToDateFromSun | Start: Last Sunday. |
End: Today. | YearToDate |
Start: Jan 1 of the current year. | End: Today. |