Topic group Related topics Example
Searches a table for the first record that matches a specified condition.
[FOR <condition 1>]
[WHILE <condition 2>]
FOR <condition 1>
WHILE <condition 2>
The scope of the command. The default scope is ALL. LOCATE is usually used with a FOR condition.
LOCATE performs a sequential search of a table and tests each record for a match to the specified condition. If a match is found the record pointer is left at that record. Issuing CONTINUE resumes the search, allowing additional records meeting the specified condition to be found.
Whenever a match is found, FOUND( ) returns true. If match is not found, the record pointer is left after the last record checked, which usually leaves it at the end-of-file. Also, FOUND( ) returns false.
If SET TALK is ON, LOCATE will display the record number of the matching record in the result pane of the Command window if you are searching a DBF table. If no match is found, LOCATE will display "End of Locate scope"
Because the default scope of the command is ALL, issuing LOCATE with no options will move the record pointer to the first record in the table, because that is the first matching record in that scope. However, there is no practical reason to use LOCATE in this manner. A FOR condition is usually used with LOCATE to find records that match a condition. An understanding of command scope is essesntial to using LOCATE effectively.
LOCATE does not require an indexed table; however, if an index is in use, LOCATE follows its index order. When using the = operator to compare strings, LOCATE uses the rules established by SET EXACT to determine whether the strings match. Use the == operator to perform exact matches regardless of SET EXACT.
The search commands LOCATE and SEEK are each designed for use under particular conditions. LOCATE is the most flexible, accepting expressions of any data type as input and searching any field of a table. For large tables, however, a sequential search using LOCATE might be slow.
Use SEEK or SEEK( ) for greater speed. Both conduct an indexed search, similar to looking up a topic in a book index and turning directly to the appropriate page, allowing information to be found almost immediately. Once you use the INDEX command to create an index for a table, SEEK uses this index to quickly identify an appropriate record.
You can use SEEK and LOCATE in combination. Use SEEK to quickly narrow down a search and then use LOCATE with the appropriate scope to find the exact you’re looking for.
Use the Rowset object’s beginLocate( ) and applyLocate( ) methods.