[Last Call] Learn how to a build a cloud-first strategyRegister Now


uniqueness of GUID

Posted on 2006-05-30
Medium Priority
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

Expert Comment

ID: 16795427
LVL 66

Accepted Solution

Jim Horn earned 2000 total points
ID: 16795444
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.

Expert Comment

ID: 16796436

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:



Expert Comment

ID: 16810067
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

829 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