Creating a GUID

Top  Previous  Next

An example of how to create a GUID (Globally Unique ID) in visual basic:

 

In the declaration section of a module:

 

Public Declare Function CoCreateGuid Lib "ole32.dll" (pGUID As Any) As Long

 

Then in the module itself:

 

Public Function CreateGUID() As String

   Dim i As Long, b(0 To 15) As Byte

   If CoCreateGuid(b(0)) = 0 Then

       For i = 0 To 15

           CreateGUID = CreateGUID & Right$("00" & Hex$(b(i)), 2)

       Next i

   Else

       'In the unlikely event of an error use this

       CreateGUID = Format$(Now, "hhnnssmmddyyyy") & Timer

   End If

End Function