>Hi Gang!
>
>We used to have a record in a table for EACH lane of a drug store (each register, that is). Each record had a field to track the sales number for THAT register (lane). So, you could have multiple duplicate sales numbers in the drug store over a period of time.
>
>They didn't like that ( I didn't either ), so I was tasked to code up a solution so there would be UNIQUE sales numbers at that location (store). The registerrs would no longer look at the previous table, but would call a function to get a UNIQUE sales number for the store at THAT MOMENT in time. Here is what I came up with, ...
>
>SET REPROCESS TO AUTO is globally set at the beginning of the program.....
>
>
>FUNCTION GetNextSaleNumber
>LOCAL lnNextSaleNumber
>
>IF NOT USED('sales_no')
> USE IN 0 sales_no
>ENDIF
>*Lock the sales_no table to allow no one else to modifiy the last_sale value while I am updating it....
>IF FLOCK('sales_no')
> SELECT sales_no
> IF sales_no.last_sale = 999999
> REPLACE sales_no.last_sale WITH 1
> ELSE
> REPLACE sales_no.last_sale WITH sales_no.last_sale + 1
> ENDIF
> lnNextSaleNumber = sales_no.last_sale
>ELSE
> lnNextSaleNumber = -1
>ENDIF
>SELECT sales_no
>UNLOCK
>RETURN lnNextSaleNumber
>
>
>Sales_no is a simple table with ONE record and ONE field in it. ( sales_no.last_sale ).
>
>And you would call it this way.... lnSaleNo = GetNextSaleNumber()
>
>Does this look ok? Should I do some error trapping in case something goes wroing ( a pc locks up during the function and LOCKS the sales_no table ) ??
>
>What else would you do?
>
>Thanks!
One thing that doesn't look ok, is when you fail to lock the table, you return -1
You should at least do a number of attemps to lock the table, imo
Gregory