SAP Knowledge Base Article - Public

1582308 - Error: 'A subscript must be between 1 and the size of the array.' when refreshing a report in Crystal Reports

Symptom

  • Error refreshing a report.
  • Same report used to refresh successfully before.
  • When refreshing a report in Crystal Reports, it fails with the error:
        
       Error: 'A subscript must be between 1 and the size of the array.'
         
      
       subscript array error.png 

Environment

  • SAP Crystal Reports 2008
  • SAP Crystal Reports 2011
  • SAP Crystal Reports 2013
  • SAP Crystal Reports 2016

Reproducing the Issue

  1. In Crystal Reports, create a report off any data source.
         
  2. Create a formula that insert values in an array, like:
       
    Whileprintingrecords;
    StringVar Array myArray;
    NumberVar x := x + 1;
       
    Redim Preserve myArray[x];
    myArray[x] := <{My Database Field}>; 
                 
  3. Insert the formula in the Details section of the report.
          
  4. Create a second formula to display all the values from the array, like:
    1.    
    2. Whileprintingrecords;
      StringVar Array myArray;
      Join(myArray,chr(13))
    3.   
  5. Insert the formula in the Report Footer section of the report.
        
  6. When refreshing the report, it fails with the error: 'A subscript must be between 1 and the size of the array'

Cause

  • The error occurs because a formula reach a limit for an Array; or a String.
           
  • The limitations are:
    • Array can contain up to a maximum of 1000 elements.
    • String can contain up to a maximum of 254 characters.
       
           
  • If it has not reach the above limits, then, it's because the Array or String contains less elements or characters than what is referenced in the formula.
       
    For example: If we have an array with 10 values, but we have a formula attempting to access value at position 20 of the array, it will fail, because the array was defined to be 10 elements only, and not 20.

Resolution

  • In order to avoid referencing an array or a string, outside of it's limit, update the formula to verify if the limit has been reached.
      
    • For a String:
      Add a condition in a formula that verify you are not going over the limit of 254 characters, or over the actual number of characters the string contains. Example, a formula to check if we reach the limit will look like:   

         WhilePrintingRecords;
         StringVar myString;
         NumberVar x := x + 1;
         If x <= 254 Then myString[x]

                 
    • For an Array:
      Add a condition to verify yo are not going over 1,000 element, as well as to verify you are not going over the number of elements defined for the array, as it could be less than 1,000. And if you really need to store more than 1,000 element, create a second array to store the next 1,000 values. The formula will look like: 

         WhilePrintingRecords;
         StringVar Array myArray1;
         StringVar Array myArray2;

         StringVar myValue := "my value";
         NumberVar x := x + 1;

         If x <= 1000 Then
         (
            Redim Preserve myArray1[x];
            myArray1[x] := myValue;
         )
         Else if x <= 2000 Then
         (
            Redim Preserve myArray2[x-1000];
            myArray2[x-1000] := myValue;
         )

        
           

Keywords

CR, array , KBA , BI-RA-CR , Crystal Reports designer or BusinessViews Manager , Problem

Product

Crystal Reports 2008 V1 ; SAP Crystal Reports 2011 ; SAP Crystal Reports 2013 ; SAP Crystal Reports 2016