select out scan nkey = GenerateKeyValue('tkt') select tkt append blank replace tkt_pk with nkey select out endscan the first call to the GenerateKeyValue function is returning the wrong value. it is returning 2000 when it needs to return 2001. after the first call it works fine. there are some other places in the project where off the wall values are returned that aren't even in the key value table. below is the GenerateKeyValue function. any help is appreciated. thank you very much! *=================================================================== function GenerateKeyValue lparameter cKeyFile *=================================================================== if parameters() = 0 or empty(cKeyFile) messagebox("Failure in Primary Key Generation. " + chr(13) + ; "Introducing Error to Halt Execution. " + chr(13) + ; "Call *****.") set step on endif * Declare local variables: local yCurArea && Current work area upon entry local yCurRepro && Setting of 'reprocess' upon entry local lGotIt && Record lock successful local tStart && Starting number of seconds * Save the current work area: yCurArea = select() * Initialize the values table: select 0 if not used('rx23') use gAdvDir + "\arm\dat\rx23" endif select rx23 set order to spc in rx23 * Locate the specified value record: if not seek( padr( cKeyFile, 5 )) * Record not found; create it: insert into rx23 values( padr(alltr(cKeyFile),5), 0, 1, 0, 0 ) endif * Calculate the generated number: tStart = seconds() do while not lGotIt if rlock() replace rx23.ia with iif( ; ( ( rx23.ia + rx23.ib ) > rx23.ic ) and ( rx23.ic <> 0 ), ; rx23.id + rx23.ib, ; rx23.ia + rx23.ib ) lGotIt = .T. unlock else if (seconds() - tStart) > 20 then messagebox("Failure in Primary Key Generation. " + chr(13) + ; "Introducing Error to Halt Execution. " + chr(13) + ; "Call *****.") set step on endif endif enddo * Clean up: select (yCurArea) * Return the generated value: return rx23.ia