LPARAMETERS tcIDType, tnNumToGet LOCAL llNotUsed, lnArea, lnRetVal llNotUsed = .F. llCanLock = .F. lnArea = SELECT() IF !USED( "idcount" ) USE idcount IN 0 llNotUsed = .T. ENDIF SELECT idcount SET ORDER TO TAG table *** Lock the entire table DO WHILE !FLOCK() ENDDO SEEK UPPER( tcIDType ) IF FOUND() *** If we find the record then return this value lnRetVal = idcount.id REPLACE id WITH lnRetVal + IIF( EMPTY( tnNumToGet ), 1, tnNumToGet ) ELSE *** Record not found do create one INSERT INTO idcount ( id, table, tdesc ) VALUES ( 2, tcIDType ) lnRetVal = 1 ENDIF FLUSH UNLOCK IF llNotUsed USE IN idCount ENDIF SELECT( lnArea ) RETURN lnRetValUnfortunately, I'm getting duplicates as a result. They're really wierd as they're in batches, for instance I might get keys 316202,316203,316204,316205 for records 100-103 and then 316202,316203,316204,316205 for 104-107 and then the next key generated is 316206 and everything is synchronised again. Any help is appreciated.