This article was previously published under Q92359
The LOOKUP() function searches a database for the first occurrence ofa specified expression. If FoxPro finds the expression, it moves therecord pointer to the record that corresponds to the expression andreturns the contents of the specified field in the record. If nomatching record is found, the function returns the NULL string andleaves the record pointer at the end of the file.
The LOOKUP() function can be used either to search for a specificexpression in a database or to search for the first occurrence of amatching field in a second database.
The syntax of the LOOKUP() function is as follows
<field1> is the field to return. <expr> is the string to search for. <field2> is the field to search. <expC> is the compact index tag to use in the search (optional).
The following code searches the CUSTOMER.DBF file for the firstoccurrence of "Mark Cummings" in the CONTACT field. If LOOKUP() findsa matching record, it returns the contents of the COMPANY field.
USE CUSTOMER ?LOOKUP(COMPANY,"Mark Cummings",CONTACT)
The following code uses both the CUSTOMER.DBF and INVOICES.DBF files.LOOKUP() moves the record pointer through the CUSTOMER database untilit finds a record that matches the current CNO field in INVOICES.LOOKUP() returns the contents of the COMPANY field in the CUSTOMERdatabase.
SELECT A USE INVOICES SELECT B USE CUSTOMER ?LOOKUP(COMPANY,INVOICES.CNO,CNO)
If LOOKUP() includes the name of a compact index tag in the optionalcharacter expression <expC>, the search expression should match theindex expression for the compact index tag. You can search thedatabase for the contents of one field or a combination of fields byincluding the name of a compact index tag. The following code adds theCONTACT tag to the first example above:
When the LOOKUP() expression does not specify a tag and the databaseis indexed, FoxPro searches the primary index for the expression. Ifno index is available, the LOCATE command is used to search thedatabase.
NOTE: The Rushmore query-optimization system cannot optimize aLOOKUP() call.