Author: Ernesto De Spirito
A function to search text in part of a field of any dataset
Answer:
The following function searches for text in any part of a field of any dataset (it
can be for example a TTable, TQuery, TADOTable, TADOQuery, TIBTable, TIBQuery,
etc.)
1 type2 TLocateStrOption = (loCaseSensitive, loContinue);
3 TLocateStrOptions = setof TLocateStrOption;
4 5 function LocateStr(Dataset: TDataset; Field: TField; Str: string;
6 LocateOptions: TLocateStrOptions): boolean;
7 // Searches text in any part of a dataset field. The search can be8 // case sensitive (option loCaseSensitive) and can start from the9 // beginning or from the current record (option loContinue).10 //11 // Returns True if the string was found (the dataset is positioned12 // in that record) and False otherwise (the dataset is left in EOF)13 var14 ControlsDisabled: boolean;
15 begin16 ControlsDisabled := Dataset.ControlsDisabled;
17 ifnot ControlsDisabled then18 Dataset.DisableControls;
19 try20 if loContinue in LocateOptions then21 begin22 ifnot Dataset.Eof then23 Dataset.Next;
24 end25 else26 Dataset.First; // Start from the beginning27 ifnot (loCaseSensitive in LocateOptions) then28 Str := UpperCase(Str);
29 whilenot Dataset.Eof do30 begin31 if loCaseSensitive in LocateOptions then32 begin33 if Pos(Str, Field.AsString) <> 0 then34 break;
35 end36 else37 begin38 if Pos(Str, UpperCase(Field.AsString)) <> 0 then39 break;
40 end;
41 Dataset.Next;
42 end;
43 Result := Dataset.Eof;
44 finally45 ifnot ControlsDisabled then46 Dataset.EnableControls;
47 end;
48 end;
Copyright (c) 2001 Ernesto De Spirito
Visit: http://www.latiumsoftware.com/delphi-newsletter.php