Thread: FoxPro Some General Questions/Serial numbers

Serial numbers
FUNCTION SerialNumber(tcSequence)
   * Get serial number. Used mainly to generate primary keys.
   * The easiest way to achieve this is to call this function from a fields default value.
   * Example: default value for primary key "Client" of table "Client" = SerialNumber("Client")
   * This function accesses table SerialNumber. Fields: Sequence C(30), NextNum I.
   tcSequence = lower(tcSequence)
   local lnSelect
   lnSelect = select()
   if used("serialnumber")
      select serialnumber
      select 0
      use serialnumber
   set order to "sequence"
   seek padr(tcSequence, len(sequence))
   if not found()
      append blank
      replace sequence with tcSequence, nextnum with 1
   local lnReturnValue
   if lock()
      lnReturnValue = nextnum
      replace nextnum with nextnum + 1
      lnReturnValue = -1
   select (lnSelect)
   return lnReturnValue