uniqueness of GUID

Posted on 2006-05-30
Last Modified: 2010-04-07
with a GUID - are they really unique?

is there a guarantee that GUID's in a system will actually be unique?  whats the best way to generate them using VB?
Question by:kenshaw
    LVL 6

    Expert Comment

    LVL 65

    Accepted Solution

    Definition of Globally Unique Identifier (GUID)

    >is there a guarantee that GUID's in a system will actually be unique?
    (2128 or 3.4028×1038) is so large that the possibility of the same number being generated twice is very small.

    >whats the best way to generate them using VB?
    If you are building an exe or dll, one will be automatically assigned at compile time.
    If you are intending to use one as the primary key of a record in a table, then the hosting database can likely generate one.

    Hope this helps.
    LVL 6

    Expert Comment


    The idea of a GUID is that no two machines can ever generate the same GUID value twice. This is achieved by using a combination of the current time, your machine's Media Access Control (MAC) address (a unique number built into all network cards) if it has one, and other routines.

    Here is a sample code that can be used to generate a GUID:

    LVL 5

    Expert Comment

    Private Declare Function CoCreateGuid Lib "ole32" (id As Any) As Long
    Private Sub Form_Load()
        MsgBox "Generated GUID: " + CreateGUID
    End Sub
    Public Function CreateGUID() As String
        Dim id(0 To 15) As Byte
        Dim Cnt As Long, GUID As String
        If CoCreateGuid(id(0)) = 0 Then
            For Cnt = 0 To 15
                CreateGUID = CreateGUID + IIf(id(Cnt) < 16, "0", "") + Hex$(id(Cnt))
            Next Cnt
            CreateGUID = Left$(CreateGUID, 8) + "-" + Mid$(CreateGUID, 9, 4) + "-" + Mid$(CreateGUID, 13, 4) + "-" + Mid$(CreateGUID, 17, 4) + "-" + Right$(CreateGUID, 12)
            MsgBox "Error while creating GUID!"
        End If
    End Function

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now