Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: FoxPro FAQ

Usefull Functions & Procedures

How to obtain a guaranteed unique ID number by Mike Gagnon
Posted: 31 Aug 02 (Edited 2 Nov 02)

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.

* -- 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

* -- 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) + ;

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

   * -- Convert the final 12 characters of the GUID and store in data5
   FOR nGuidLen = 1 TO 6

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

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


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

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)
LOCAL lnByte, lnRetval
lnRetval = 0
FOR lnByte = 0 TO 24 STEP 8
   lnRetval = lnRetval + (ASC(lcLongstr) * (2^lnByte))
   lcLongstr = RIGHT(lcLongstr, LEN(lcLongstr) - 1)
RETURN lnRetval  

Mike Gagnon

Back to Microsoft: FoxPro FAQ Index
Back to Microsoft: FoxPro Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close