FLOCK( ) Function

Attempts to lock the current or specified table.

FLOCK([nWorkArea | cTableAlias])

Return Values

Logical

Parameters

  • nWorkArea
    Specifies the work area of the table that FLOCK( ) attempts to lock.

    FLOCK( ) returns false (.F.) if a table isn't open in the work area you specify.

  • cTableAlias
    Specifies the alias of the table that FLOCK( ) attempts to lock.

    Microsoft Visual FoxPro generates an error message if you specify a table alias that doesn't exist.

Remarks

FLOCK( ) returns true (.T.) if the table is successfully locked and returns false (.F.) if the table or a record in the table is already locked by another user.

If you omit the optional arguments, FLOCK( ) attempts to lock the table open in the currently selected work area.

Note   If FLOCK( ) fails to lock a table, it returns false (.F.) and does not generate an error. As a result, you can't use FLOCK( ) to trigger an ON ERROR routine.

When a table is locked, the table is available for both read and write access by the user who placed the lock. Other users on the network have read-only access to the table. For information on how to lock a table and prevent access to it by other users, see SET EXCLUSIVE and USE.

A table remains locked until it is unlocked by the user who placed the lock. The table can be unlocked by issuing UNLOCK, closing the table, or quitting Visual FoxPro. Tables can be closed with USE, CLEAR ALL, or CLOSE DATABASES.

By default, FLOCK( ) attempts to lock a table once. Use SET REPROCESS to automatically retry a table lock when the first attempt fails. SET REPROCESS determines the number of lock attempts, or the length of time during which lock attempts are made when the initial lock attempt is unsuccessful. For more information, see SET REPROCESS.

You can establish relations between two or more tables with SET RELATION. Placing a file lock on a table that is related to one or more tables doesn't place a file lock on the related tables. You must explicitly place and remove locks on the related tables.

For additional information about record and file locking and sharing tables on a network, see Programming for Shared Access.

Example

CLOSE DATABASES
OPEN DATABASE (HOME(2) + 'Data\testdata')
USE products  && Opens products table
SET REPROCESS TO 3 SECONDS
SELECT * FROM products INTO TABLE newprods

IF FLOCK( )

** New product initialization ** REPLACE ALL in_stock WITH 0.00 REPLACE ALL on_order WITH 0.00 WAIT 'Initialization Complete' WINDOW NOWAIT ELSE ** File is locked, warn user ** WAIT WINDOW 'Unable to open products file; try again later!' NOWAIT ENDIF

BROWSE FIELDS in_stock, on_order && Displays newprods table
USE
ERASE newprods.dbf

See Also

LOCK( ) | RLOCK( ) | SET REPROCESS | SET RELATION | UNLOCK | USE | SET EXCLUSIVE