compile function next_key( Table, TableHandle, Operation, Key )
begin condition
pre:
post:
end condition
* this function will return the next available key number for a sequentiall
* keyed table...It will get a lock and hold it so the calling routine will
* be responsible for unlocking
$insert logical
$insert msg_equates
declare function msg

if Operation = 'NEXT' then
	NextRecId = xlate( 'DICT.':Table, '%SK%', 1 , 'X' )
	Locked = false$
	loop
	    if rowexists( Table, NextRecId ) then
	        NextRecId += 1 
	    end else
		    lock TableHandle, NextRecId then
		        Locked = true$
		    end else
		        NextRecId += 1
		    end
		end
	  until Locked
	repeat
	return NextRecId
end else
    open 'DICT.':Table to DictTable else
        MsgInfo = ''
        MsgInfo<mcol$>  = -2
        MsgInfo<mrow$>  = -2
        MsgInfo<mtext$> = 'Unable to open DICT.':Table:'...'
        Void = msg( '', MsgInfo )
        return 0
    end
    Locked = false$
    loop
        lock DictTable, '%SK%' then
            Locked = true$
        end
      until Locked
    repeat
    NewKey = Key+1
    write NewKey on DictTable, '%SK%' else
        MsgInfo = ''
        MsgInfo<mcol$>  = -2
        MsgInfo<mrow$>  = -2
		MsgInfo<mtext$> = 'Unable to write ':NewKey:' DICT.':Table:' %SK%...'         
        Void = msg( '', MsgInfo )
        return 0
    end
    unlock DictTable, '%SK%' else
        MsgInfo = ''
        MsgInfo<mcol$>  = -2
        MsgInfo<mrow$>  = -2
		MsgInfo<mtext$> = 'Unable to unlock DICT.':Table:'...'
        Void = msg( '', MsgInfo )
        return 0
    end
    return 0
end   

  

