Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to obtain a guaranteed unique ID number

Usefull Functions & Procedures

How to obtain a guaranteed unique ID number

by  Mike Gagnon  Posted    (Edited  )
A GUID, or globally unique identifier, is a number that Microsoft guarantees is unique throughout the entire world. It is best used when you need a primary key that is absolutely, positively unique, and you have distributed systems in which data is entered.

For example, if you have a mobile sales force that uses laptop computers to enter sales orders and customers in the field, then uploads that data to a central system, simply assigning incremental integer values in the field will cause multiple laptops submitting identical key values. If, instead, the laptop programs generated a GUID, then you can be assured that even when the data is combined with data from other users from around the world each record's key (the GUID) will be unique.

Downside to using GUIDs: besides making an API call, GUIDs consume 36 bytes of storage - much larger than an integer. Therefore, they should only be used where you need to ensure unique values across a distributed system, or believe that someday you may be joining data from currently separate systems.

This code is based on a Microsoft article located at: http://support.microsoft.com/support/kb/articles/Q269/3/87.asp
article (c) 2000 Microsoft Corp.


Code:
* -- returns a Globally Unique IDentifier (GUID)

local cData1, cData2, cData3, cData4, cData5, cDataAll
private PGuid

store "" to cData1, cData2, cData3, cData4, cData5, cDataAll

* -- declare external function
DECLARE INTEGER CoCreateGuid IN OLE32.DLL STRING @pGuid

* -- Initialize the buffer that will hold the GUID with nulls
pGuid = REPLICATE(CHR(0),17)

* -- Call CoCreateGuid

IF CoCreateGuid(@pGuid) = 0   && success

   * -- Store the first eight characters of the GUID in data1
   cData1 = RIGHT(TRANSFORM(strtolong(LEFT(pGuid,4)),"@0"),8)

   * -- Store the first group of four characters of the GUID in data2
   cData2 = RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,5,2)),"@0"),4)

   * -- Store the second group of four characters of the GUID in data3
   cData3 = RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,7,2)),"@0"),4)

   * -- Store the third group of four characters of the GUID in data4
   cData4 = RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,9,1)),"@0"),2) + ;
      RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,10,1)),"@0"),2)

   * -- Initialize data5 to a null string
   cData5 = ""

   * -- Convert the final 12 characters of the GUID and store in data5
   FOR nGuidLen = 1 TO 6
      cData5=cData5+RIGHT(TRANSFORM(strtolong(SUBSTR(pGuid,10+nGuidLen,1))),2)
   ENDFOR

   * -- Check the length of data5. If less than 12, the final 12-len(data5)
   * -- characters are '0'
   IF LEN(cData5) < 12
      cData5=cData5+REPLICATE("0",12-LEN(cData5))
   ENDIF

   * -- Assemble the GUID into a string
   cDataAll = cData1+"-"+cData2+"-"+cData3+"-"+cData4+"-"+cData5

ENDIF


* -- Done with the call to CoCreateGuid, so clear the DLLs from memory
CLEAR DLLS


return cDataAll


*******************

FUNCTION strtolong
* -- Passed:  4-byte character string (lcLongstr) in low-high ASCII format
* -- Returns:  long integer value
* -- Example:
* -- m.longstr = "1111"
* -- m.longval = strtolong(m.longstr)
PARAMETERS lcLongstr
LOCAL lnByte, lnRetval
lnRetval = 0
FOR lnByte = 0 TO 24 STEP 8
   lnRetval = lnRetval + (ASC(lcLongstr) * (2^lnByte))
   lcLongstr = RIGHT(lcLongstr, LEN(lcLongstr) - 1)
NEXT
RETURN lnRetval

Mike Gagnon
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top