SCAN ... ENDSCAN Command

Moves the record pointer through the currently selected table and executes a block of commands for each record that meets the specified conditions.

SCAN [NOOPTIMIZE] [Scope] [FOR lExpression1] [WHILE lExpression2]
   [Commands] [LOOP] [EXIT] 
ENDSCAN

Parameters

  • NOOPTIMIZE
    Prevents Rushmore optimization of SCAN.

    For more information, see SET OPTIMIZE and Using Rushmore to Speed Data Access.

  • Scope
    Specifies a range of records to be scanned. Only the records within the range are scanned. The scope clauses are: ALL, NEXT nRecords, RECORD nRecordNumber, and REST. For more information on scope clauses, see the Scope Clauses online topic.

    The default scope for SCAN is all records (ALL).

  • FOR lExpression1
    Executes commands only for records for which lExpression1 evaluates to true (.T.). Including the FOR clause lets you filter out records you don't want scanned.

    Rushmore optimizes a query created with SCAN ... FOR if lExpression1 is an optimizable expression. For best performance, use an optimizable expression in the FOR clause.

    For more information, see SET OPTIMIZE and Using Rushmore to Speed Data Access.

  • WHILE lExpression2
    Specifies a condition whereby the commands are executed for as long as lExpression2 evaluates to true (.T.).

  • Commands
    Specifies the Visual FoxPro commands to be executed.

  • LOOP
    Returns control directly back to SCAN. LOOP can be placed anywhere between SCAN and ENDSCAN.

  • EXIT
    Transfers program control from within the SCAN ... ENDSCAN loop to the first command following ENDSCAN. EXIT can be placed anywhere between SCAN and ENDSCAN.

  • ENDSCAN
    Indicates the end of the SCAN procedure.

Remarks

SCAN automatically advances the record pointer to the next record that meets the specified conditions and executes the block of commands.

You can place comments after ENDSCAN on the same line. The comments are ignored during program compilation and execution.

SCAN ... ENDSCAN ensures that, upon reaching ENDSCAN, Visual FoxPro reselects the table that was current when the SCAN ... ENDSCAN loop began.

Example

The following example uses a SCAN ... ENDSCAN loop to display all the companies in Sweden.

CLOSE DATABASES
OPEN DATABASE (HOME(2) + 'Data\testdata')
USE customer  && Opens Customer table
CLEAR

SCAN FOR UPPER(country) = 'SWEDEN'
   ? contact, company, city
ENDSCAN

See Also

DO CASE ... ENDCASE | DO WHILE ... ENDDO | FOR EACH ... ENDFOR | FOR ... ENDFOR