Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to obtain GUIDs 1

Status
Not open for further replies.

Custardptt

Technical User
Jan 12, 2005
31
GB
Hi All,

I need to use GUIDs to communicate with an external database. How can I create GUIDs within MS Access / VBA.

Thanks in advance

Pete
 
Code:
Option Compare Database
Option Explicit

 Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
End Type

Private Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) As Long

Function GetGUID() As String
Dim udtGUID As GUID
If (CoCreateGuid(udtGUID) = 0) Then
    GetGUID = _
    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 If
End Function
 
Code:
Function newguidx()

    With CreateObject("Scriptlet.TypeLib")
    newguidx = Left(.GUID, 38)
    End With
   
End Function
 
If you set a an Autonumber field's size to Replication ID, you will bet a value generated on the lines of:
{221D633D-EB90-459B-A4B9-C42FE875F662}

You can use code with the value:

Code:
Sub CheckGUIDType()
'From Help
    Dim dbs As Database, rst As dao.Recordset

    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("TableHere")
    Debug.Print rst!Guid
    Debug.Print TypeName(rst!GuidFieldName)
    Debug.Print TypeName(GUIDFromString(rst!GuidFieldName))
End Sub
 
Thanks All,

Pwise's solution was ideal, Thanks Remou as well.

Pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top