Icon Setting Ranges on Tables

Introduction
Setting ranges on tables is accomplished through several methods of the TDBISAMTable component. The basic range methods include the SetRange, SetRangeStart, SetRangeEnd, EditRangeStart, EditRangeEnd, and ApplyRange methods. The KeyFieldCount property is used with the SetRangeStart, SetRangeEnd, EditRangeStart and EditRangeEnd methods to control searching using the ApplyRange method. All range operations are dependent upon the active index order set using the IndexName or IndexFieldNames properties. Ranges may be combined with expression filters set using the Filter and Filtered propertes and/or callback filters set using the OnFilterRecord event to further filter the records in the table.

Setting a Range
The SetRange method accepts two arrays of values to use in order to set a range on a given table. If the current record pointer does not fall into the range values specified, then the current record pointer will be moved to the nearest record that falls within the range. These value arrays must contain the field values in the same order as the field names in the active index or the range will not return the desired results. However, SetRange does not require that you fill in all of the field values for all of the fields in the active index, rather only that you fill in the field values from left to right. The following example shows how to perform a range on the primary index comprised of the CustomerNo field:

begin
   with MyTable do
      begin
      { Set to the primary index }
      IndexName:='';
      { Set a range from customer 100 to customer 300 }
      SetRange([100],[300]);
      end;
end;

The SetRangeStart, SetRangeEnd, EditRangeStart, and EditRangeEnd methods are used in conjunction with the ApplyRange method to perform a range using field assignments instead of arrays of field values. The SetRangeStart method begins the range process by putting the TDBISAMTable component into the dsSetKey state and clearing all field values. You can examine the state of the table using the State property. The application must then assign values to the desired fields for the start of the range and then proceed to call SetRangeEnd to assign values to the desired fields for the end of the range. After this is done the application can call the ApplyRange method to perform the actual range operation. The EditRangeStart and EditRangeEnd methods extend or continue the current range process by putting the TDBISAMTable component into the dsSetKey state but not clearing any field values. You can examine the state of the table using the State property. This allows you to change only one field without being forced to re-enter all field values needed for the beginning or ending values of the range. The KeyFieldCount property controls how many fields, based upon the active index, are to be used in the actual range and can be set independently for both the starting and ending field values of the range. By default the KeyFieldCount property is set to the number of fields in the active index. The following example shows how to perform a range using the SetRangeStart, SetRangeEnd, and ApplyRange methods and KeyFieldCount property. The active index is a secondary index called "CustomerName" that consists of the CustomerName field and the CustomerNo field:

begin
   with MyTable do
      begin
      { Set to the CustomerName secondary index }
      IndexName:='CustomerName';
      { Set a range to find all customers with
        a name beginning with 'A' }
      SetRangeStart;
      FieldByName('CustomerName').AsString:='A';
      { This causes the range to only look at
        the first field in the current index }
      KeyFieldCount:=1;
      SetRangeEnd;      
      { Note the padding of the ending range
        values with lowercase z's
        to the length of the CustomerName
        field, which is 20 characters }
      FieldByName('CustomerName').AsString:='Azzzzzzzzzzzzzzzzzzz';
      { This causes the range to only look at
        the first field in the current index }
      KeyFieldCount:=1;
      ApplyRange;
      end;
end;

Information In the previous example we executed a partial-field range. What this means is that we did not include all of the fields in the active index in the range. DBISAM does not require that you use all of the fields in the active index for the range.
Image