>>*!***************************************************************** >>* PROGRAM : LNEXTID.PRG >>* AUTHOR : DOUGLAS N. GRAY >>* DESC : Unique id generator. >>* PARAMETER : <None> >>* CREATED : 02/20/96 >>* MODIFIED : 11/12/1999 - Changed PRIVATE to LOCAL, changed field names. >>* MODIFIED : Nadya Nosonovsky 08/22/2000 12:38:41 PM >>*!***************************************************************** >>procedure LNextID >>lparameters tcalias >>local lnoldarea,lnnextid, lcDBC, lcOldSetDBC, llOpened >>llOpened=.t. >>*--- Variable Setup >>tcalias = iif(empty(m.tcAlias),upper(alias()),upper(tcalias)) >>lnoldarea = select() >>lnnextid = 0 >>lcDBC=cursorgetprop('database',tcalias) && Return the name of owning database >>if empty(lcDBC) >> =messagebox('You can not assign Next ID to the free table!',16,'Warning') >> return .f. >>endif >>lcOldSetDBC=set('database') && Save current status >>if !dbused(lcDBC) >> open data (lcDBC) >> llOpened=.f. && Was not open before >>endif >>set database to (lcDBC) && Make this DBC current >>*--- Go to NextID Database to assign the next unique ID >>*--- Assumes the you are sitting in the correct DBC. >>if !used('NEXTID') >> use NEXTID in 0 >> select NEXTID >>else >> select NEXTID >>endif >>set order to table && NOTE: Index expression = UPPER(table) >> >>if !seek(tcalias) >> insert into NEXTID (table,id) values (tcalias,1) >> lnnextid = 1 >>else >>*--- RLOCK will attempt the lock indefinately, because it would be bad to return 0 (the alternative). >>*--- User will be able to cancel attempt if in a deadlock by pressing the ESC key. >> if rlock() >> replace NEXTID.id with NEXTID.id + 1 >> lnnextid = NEXTID.id >> unlock >> else >> lnnextid=0 >> endif >>endif >> >>*--- Return to prior workarea. >>select (m.lnoldarea) >>if not llOpened >> close database >>endif >>if !empty(lcOldSetDBC) >> set database to (lcOldSetDBC) >>endif >>return lnnextid>>>