Searching DataSets in Delphi


If a dataset is not unidirectional, you can search against it using the Locate and Lookup methods.   These methods enable you to search on any type of columns in any dataset.


moves the cursor to the first row matching a specified set of search criteria.   In its simplest form, you pass Locate the name of a column to search, a field value to match, and an options flag specifying whether the search is case-insensitive or if it can use partial-key matching. (Partial-key matching is when the criterion string need only be a prefix of the field value.)   For example, the following code moves the cursor to the first row in the CustTable where the value in the Company column is "Professional Divers, Ltd.":


var

        LocateSuccess: Boolean;

        SearchOptions: TLocateOptions;

begin

        SearchOptions := [loPartialKey];

        LocateSuccess := CustTable.Locate

        ('Company', 'Professional Divers,

        Ltd.', SearchOptions);

end;


If Locate finds a match, the first record containing the match becomes the current record. Locate returns True if it finds a matching record, False if it does not.  

 

If a search fails, the current record does not change.


Because search values are Variants, if you pass multiple values, you must either pass a Variant array as an argument (for example, the return values from the Lookup method), or you must construct the Variant array in code using the VarArrayOf function. The following code illustrates a search on multiple columns using multiple search values and partial-key matching:


with CustTable do

        Locate('Company;Contact;Phone',

        VarArrayOf(['Sight Diver','P']),

        loPartialKey);


Locate uses the fastest possible method to locate matching records. If the columns to search are indexed and the index is compatible with the search options you specify, Locate uses the index.