Pagebanner

ThreeWave Creating A Unique Indentifier With A GUID

This page describes how to create a globally unique indentifier.

 

Creating A GUID

If you need a unique identifier that is certain to be unique across all applications and all computers and all users, you can create a "Globally Unique Indentifier", or GUID. This identifier is universally unqiue. Note that while this value will be unique, there is no meaningful information contained within it. That is, you cannot examine the value of a GUID and discern any information about when or where it was created. The code for creating a GUID is shown below:

Private Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) As Long

Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

Public Function CreateGUID() As String
    Dim G As GUID
    With G
    If (CoCreateGuid(G) = 0) Then
    CreateGUID = _
        String$(8 - Len(Hex$(.Data1)), "0") & Hex$(.Data1) & _
        String$(4 - Len(Hex$(.Data2)), "0") & Hex$(.Data2) & _
        String$(4 - Len(Hex$(.Data3)), "0") & Hex$(.Data3) & _
        IIf((.Data4(0) < &H10), "0", "") & Hex$(.Data4(0)) & _
        IIf((.Data4(1) < &H10), "0", "") & Hex$(.Data4(1)) & _
        IIf((.Data4(2) < &H10), "0", "") & Hex$(.Data4(2)) & _
        IIf((.Data4(3) < &H10), "0", "") & Hex$(.Data4(3)) & _
        IIf((.Data4(4) < &H10), "0", "") & Hex$(.Data4(4)) & _
        IIf((.Data4(5) < &H10), "0", "") & Hex$(.Data4(5)) & _
        IIf((.Data4(6) < &H10), "0", "") & Hex$(.Data4(6)) & _
        IIf((.Data4(7) < &H10), "0", "") & Hex$(.Data4(7))
    End If
    End With
End Function    

You can then call this function with code like:

Dim UniqueID As String
UniqueID = CreateGUID()
ShortFadeBar
LastUpdate This page last updated: 20-March-2009.

Created by Chip Pearson at Pearson Software Consulting, LLC
Email: chip@cpearson.com Before emailing me, please read this page
http://www.cpearson.com/excel/CreateGUID.aspx
Copyright © 1997 - 2009, Charles H. Pearson

Submit bug information or errors on the Bug And Error Report Page.



 


sectionbreak
Essential Tools For Developers


  

Essential Tools For Financial Analysts And Accounting Professionals

  
Ready


Advertise Your Product On This Site