Thread: FoxPro Some General Questions/Serial numbers

Serial numbers
Click
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
   else
      select 0
      use serialnumber
   endif
   set order to "sequence"
   seek padr(tcSequence, len(sequence))
   if not found()
      append blank
      replace sequence with tcSequence, nextnum with 1
   endif
   local lnReturnValue
   if lock()
      lnReturnValue = nextnum
      replace nextnum with nextnum + 1
   else
      lnReturnValue = -1
   endif
   unlock
   select (lnSelect)
   return lnReturnValue
ENDFUNC