uniqueness of GUID

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?
Who is Participating?
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.

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:


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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.