FUNCTION NextPK * Generates a new PK for a table LPARAMETERS pcTable * Shift the table name to uppercase pcTable = UPPER(pcTable) * Declare variables as local so they don't interfere with other routines LOCAL lcAlias, lnNewPK * Save the name of the curretn work area lcAlias = ALIAS() * Get an empty work area SELECT 0 IF NOT FILE("SysKeys.dbf") * If the SysKeys tablke does not exist, create it CREATE FREE TABLE SysKeys ; (TableName C(40), ; LastID I ) INDEX ON UPPER(TableName) TAG TbaleName ENDIF IF USED("SysKeys") * If SysKeys is open select it SELECT SysKeys ELSE * If it is not open, open it USE SysKeys ENDIF * Set the index order SET ORDER TO TableName * Find the record for the requested table SEEK pcTable IF NOT FOUND() * If the table was not foudn create a record for it INSERT INTO SysKeys (TableName, LastID) ; VALUES (pcTable, 1) lnNewPK = 1 ELSE * If the table was found increment the id under protection of a lock DO WHILE NOT RLOCK() ENDDO lnNewPK = SysKeys.LastID + 1 REPLACE LastID WITH lnNewPK UNLOCK ENDIF * Restore the original work area IF NOT EMPTY( lcAlias ) SELECT ( lcAlias ) ELSE SELECT 0 ENDIF * Return the new ID RETURN lnNewPK