Partager via


LOOKUP( ) Function

Searches a table for the first record with a field matching the specified expression.

LOOKUP(ReturnField, eSearchExpression, SearchedField [, cTagName])

Parameters

  • ReturnField
    Specifies the field whose contents LOOKUP( ) returns when the search is successful. If the search is unsuccessful, LOOKUP( ) returns the default unassigned value of the ReturnField data type.

  • eSearchExpression
    Specifies the search expression. The search expression is usually the contents of a field in the table. Also, it may correspond to the index expression of the active index or compound index tag.

  • SearchedField
    Specifies the field to search. If the table does not have an active index, LOOKUP( ) performs a sequential search through the field specified with SearchedField.

    If an index file or index tag is open whose index key expression is the search field you specify, LOOKUP( ) uses the index file or index tag to perform a faster search.

  • cTagName
    Specifies the name of a compound index tag for LOOKUP( ) to use in the search. A compound index search is the fastest search LOOKUP( ) can perform.

Return Value

Character, Numeric, Currency, Float, Integer, Double, Date, DateTime, or Logical.

Remarks

If the search is successful, LOOKUP( ) moves the record pointer to the matching record and returns the contents of a specified field in the record.

If LOOKUP( ) does not find the search expression, it returns a blank value the same length and data type as ReturnField. The record pointer is positioned at the end of the file.

If you use LOOKUP( ) to search a parent table, record pointers in all related child tables are moved to the related records.

This function cannot be optimized with Rushmore Query Optimization.

Example

In the following example, LOOKUP( ) uses the index tag company to search for the first occurrence of the string "Ernst Handel." If the search is successful, LOOKUP( ) returns the contents of the contact field and @ ... SAY displays the return value.

CLOSE DATABASES
OPEN DATABASE (HOME(2) + 'data\testdata')
USE customer ORDER company  && Open Customer table
CLEAR
@ 2,2 SAY LOOKUP(contact, 'Ernst Handel', company, 'company')

See Also

Reference

FIND Command (Visual FoxPro)

INDEX Command

LOCATE Command

SEEK Command

SEEK( ) Function

Other Resources

Functions