The following function returns the “A subscript must be between 1 and size of array”-error when run in Crystal Reports XI. Any idea why and how to fix it?
Function (optional BooleanVar start := true)
DateVar Array reportdates := [
cDate(2012, 10, 22),
cDate(2012, 11, 15),
cDate(2013, 01, 23),
cDate(2013, 02, 20),
// some more lines of dates...
cDate(2014, 01, 02)
];
// Here is some code that sorts the array just to be sure.
// Removed from question
// Find index of last reportdate not later than today
NumberVar stopIndex;
for i := 1 to UBound(reportdates) do (
if CurrentDate >= reportdates[i] then
stopIndex := i
);
DateTimeVar returnDateTime;
if start = true then ( // return start date
NumberVar startIndex;
if stopIndex = 1 then
startIndex = 1
else
startIndex = stopIndex - 1;
//*** The error occurs here
returnDateTime := cDateTime(reportdates[startIndex], cTime(0,0,0));
//*** The error occurs here
)
else ( // return stop date
DateVar stopDate = reportdates[stopIndex];
returnDateTime := dateAdd("d", -1, cDateTime(reportdates[stopIndex], cTime(23,59,59)));
);
returnDateTime;
Note:
I found that the above function returns an earlier stop date than start date if run before the second date in the array. I rewrote the function to counter that and then I did not have a situation that produced the error in question, but I would still be interested in why the error occured in this function and how to handle it.
should of course be
and now it works…
should also be changed to
to avoid errors if report is run before first report date.