Here are 2 ways to create a UID...
The 1st one produces a format that is equal to that of SQL Servers UID's.
And the 2nd way...VB Code:
Option Explicit Private Declare Function CoCreateGuid Lib "ole32" (id As Any) As Long Private Declare Function GetTickCount Lib "kernel32" () As Long Private Sub Form_Load() Dim strOld As String Dim strNew As String Dim intIndex As Long Dim lngStart As Long lngStart = GetTickCount For intIndex = 1 To 10000 strNew = CreateUID If strNew = strOld Then MsgBox "No unique! Boooooooo", vbCritical End If strOld = strNew Next intIndex MsgBox GetTickCount - lngStart & "ms", vbInformation End Sub Private Function CreateUID() As String Dim bytID(1 To 16) As Byte Dim intIndex As Integer Dim strUID As String Call CoCreateGuid(bytID(1)) For intIndex = 1 To 16 If bytID(intIndex) < CByte(16) Then strUID = strUID & "0" End If strUID = strUID & Hex$(bytID(intIndex)) Select Case intIndex Case 4, 6, 8, 10 strUID = strUID & "-" End Select Next intIndex CreateUID = strUID End Function
They both create a UID, however, the 1st chunk of code is twice as fast...VB Code:
Option Explicit Private Declare Function CoCreateGuid Lib "ole32" (id As Any) As Long Private Declare Function GetTickCount Lib "kernel32" () As Long Private Type mtypUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As Byte End Type Private Sub Form_Load() Dim strOld As String Dim strNew As String Dim intIndex As Long Dim lngStart As Long lngStart = GetTickCount For intIndex = 1 To 10000 strNew = CreateGUID If strNew = strOld Then MsgBox "No unique! Boooooooo", vbCritical End If strOld = strNew Next intIndex MsgBox GetTickCount - lngStart & "ms", vbInformation End Sub Private Function CreateGUID() As String Dim udtGUID As mtypUID Call CoCreateGuid(udtGUID) CreateGUID = _ String(8 - Len(Hex$(udtGUID.Data1)), "0") & Hex$(udtGUID.Data1) & _ String(4 - Len(Hex$(udtGUID.Data2)), "0") & Hex$(udtGUID.Data2) & _ String(4 - Len(Hex$(udtGUID.Data3)), "0") & Hex$(udtGUID.Data3) & _ IIf((udtGUID.Data4(0) < &H10), "0", "") & Hex$(udtGUID.Data4(0)) & _ IIf((udtGUID.Data4(1) < &H10), "0", "") & Hex$(udtGUID.Data4(1)) & _ IIf((udtGUID.Data4(2) < &H10), "0", "") & Hex$(udtGUID.Data4(2)) & _ IIf((udtGUID.Data4(3) < &H10), "0", "") & Hex$(udtGUID.Data4(3)) & _ IIf((udtGUID.Data4(4) < &H10), "0", "") & Hex$(udtGUID.Data4(4)) & _ IIf((udtGUID.Data4(5) < &H10), "0", "") & Hex$(udtGUID.Data4(5)) & _ IIf((udtGUID.Data4(6) < &H10), "0", "") & Hex$(udtGUID.Data4(6)) & _ IIf((udtGUID.Data4(7) < &H10), "0", "") & Hex$(udtGUID.Data4(7)) End Function
Hope this is useful to someone...
Woka




Reply With Quote