AnyDAC
ContentsIndexHome
PreviousUpNext
Finding a Record

AnyDAC offers few methods to find a specific record in the dataset.

Group
Links
General

All AnyDAC datasets are offering few approaches to find a record in a local dataset records cache. Depending on the current sorting, the record search may be optimized. 

 

Standard locating

AnyDAC datasets offer few options to locate a record or lookup a value for a specified key. These method may use the current dataset index, if it is appropriate for the method call.

  • Locate method allows to find a record for the specified key values:

 

ADQuery1.IndexFieldNames := 'ORDERID';
if ADQuery1.Locate('ORDERID', 10150, []) then
  ShowMessage('Order is found')
else
  ShowMessage('Order is not found');

 

  • Lookup method allows to lookup a value for the specified key values.

 

Extended locating

AnyDAC datasets offer few extended methods to locate a record. These method may use the current dataset index, if it is appropriate for the method call.

  • LocateEx method is similar to Locate method, but offers far more options, like search forward or backward, search from beginning or from current records, search using textual predicate (expression). For example:

 

if ADQuery1.LocateEx('Price >= 1000 and Price <= 2000', []) then
  ShowMessage('Order is found')
else
  ShowMessage('Order is not found');

 

  • LookupEx method is similar to Lookup method, but offers similar to LocateEx options.

 

Locating using a filter

This approach to locate a record was introduced into AnyDAC for compatibility with BDE components. It may be completely replaced with LocateEx method. The approach is using the methods:

  • FindFirst - find first record satisfying the predicate;
  • FindNext - find next record after current one satisfying the predicate;
  • FindPrior - find previous record after current one satisfying the predicate;
  • FindLast - find last record satisfying the predicate.

To start a search the application must specify predicate as a boolean expression in the Filter property. Note, that this method cannot be used to locate a record in a filtered dataset. Also, the filter is not activated and is used only for locating a record. For example: 

 

ADQuery1.Filter := 'Price >= 1000 and Price <= 2000';
if ADQuery1.FindFirst then
  ShowMessage('Found !');
...
if ADQuery1.FindNext then
  ShowMessage('Found !');

 

Locating using an index and a key value

This approach to locate a record was introduced into AnyDAC for compatibility with BDE components. But we found it helpful. The approach is using the methods:

  • SetKey - set dataset to dsSetKey mode, allowing to specify the index field values;
  • GotoKey - search for an exact record using the index field values, specified after SetKey method call;
  • GotoNearest - search for an exact or next record using the index field values, specified after SetKey method call;
  • FindKey - find an exact record using specified index field values;
  • FindNearest - find an exact or next record using specified index field values.

Before calling these methods the current index must be set. For example: 

 

ADQuery1.IndexFieldNames := 'ORDERID';
ADQuery1.SetKey;
ADQuery1.FieldByName('ORDERID').AsInteger := 10150;
if ADQuery1.GotoKey then
  ShowMessage('Order is found')
else
  ShowMessage('Order is not found');

 

Other options

Most of the search methods are returning True, when a record is found. Alternatively application may check the dataset Found property. 

Also note, AnyDAC does not support locating on fields of the fkCalculated and fkLookup kinds. But application may use fkInternalCalc and fkAggregate fields in locating.

What do you think about this topic? Send feedback!