FUNCTION GetKey LPARAMETERS tcTable LOCAL lnArea, llOpened, liRetVal * Save current work area lnArea = SELECT() llOpened = .F. IF !USED("Keys") USE Keys IN 0 SHARED llOpened = .T. ENDIF SELECT Keys SET ORDER TO TAG Table * FLOCK seems to work a bit better than RLOCK. * I've never had a problem with it. DO WHILE !FLOCK() ENDDO SEEK UPPER(tcTable) IF FOUND() liRetVal = NextId REPLACE NextId WITH NextId + 1 ELSE INSERT INTO Keys (Table, NextId) VALUES (tcTable, 2) liRetVal = 1 ENDIF FLUSH UNLOCK IF llOpened USE IN Keys ENDIF SELECT (lnArea) RETURN liRetVal