locate: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: locate
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Definition | |
| 2 | + | ||
| 3 | + | Unit: FreePascal, unit **db** | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | function Locate(const KeyFields: string; const KeyValues: Variant; Options: TLocateOptions): Boolean; override; | |
| 7 | + | ``` | |
| 8 | + | ||
| 9 | + | Official documentation: none | |
| 10 | + | ||
| 11 | + | ## Description | |
| 12 | + | ||
| 13 | + | **locate** looks for a record in the dataset where the specified search values for the specified fields match. If found, the function returns true and the cursor position/current record moves to that record. | |
| 14 | + | ||
| 15 | + | KeyFields can be a single field name or a semicolon-separated list of fields. | |
| 16 | + | ||
| 17 | + | KeyValue can be a variant or a variant array and the number of items must match the number of fields specified in KeyFields. | |
| 18 | + | ||
| 19 | + | Search options can be: | |
| 20 | + | ||
| 21 | + | - loCaseInsensitive: ignore upper case/lower case differences when searching | |
| 22 | + | - loPartialKey: match partial finds (instead of the complete key). Note: this only works for string-type fields, not for number/date/currency etc fields where this option is ignored. | |
| 23 | + | ||
| 24 | + | > **Note:** Locate is only implemented in non-unidirectional datasets, i.e. you must be able to move back and forwards through the dataset. | |
| 25 | + | ||
| 26 | + | > **Note:** Locate and lookup act at low level inside a dataset to search for records. Often it is more efficient to filter/limit what gets into the dataset in the first place, e.g. using SQL WHERE clauses. | |
| 27 | + | ||
| 28 | + | ## Examples | |
| 29 | + | ||
| 30 | + | Using a TDBF dataset and looking for 'di Angelo', 'Di Angelo' etc in a field called LASTNAME: | |
| 31 | + | ||
| 32 | + | ``` pascal | |
| 33 | + | uses dbf, db | |
| 34 | + | ... | |
| 35 | + | if MyDBF.Locate('LASTNAME','di Angelo',[loCaseInsensitive])then | |
| 36 | + | writeln('Found record.'); | |
| 37 | + | ``` | |
| 38 | + | ||
| 39 | + | Using multiple fields to find a record. | |
| 40 | + | ||
| 41 | + | Note: you have to add Variants to the uses clause. | |
| 42 | + | ||
| 43 | + | ``` pascal | |
| 44 | + | uses Variants; | |
| 45 | + | .. | |
| 46 | + | procedure LocateMyRecord; | |
| 47 | + | var | |
| 48 | + | aCityID, aCountryID: integer; | |
| 49 | + | begin | |
| 50 | + | if Locate('city_id;country_id', VarArrayOf([aCityID, aCountryID]), []) then | |
| 51 | + | begin | |
| 52 | + | DoSomething; | |
| 53 | + | end; | |
| 54 | + | end; | |
| 55 | + | ``` | |
| 56 | + | ||
| 57 | + | ## See also | |
| 58 | + | ||
| 59 | + | - [[lookup]] | |
| 60 | + | - [Description of how to use locate with Delphi](http://delphi.about.com/od/database/l/aa052901a.htm) | |
| 61 | + | ||
| 62 | + | --- | |
| 63 | + | *From the Free Pascal wiki, page "locate", as it stood on 2020-02-19 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
