Create a GUID in MS Access


I have many databases that contain code to create a GUID that I use for renaming and importing files. Microsoft just put out a security update that blocked Scriptlet.TypeLib from being accessed. Well here’s another way…

Add this to the top of your module up where the Option statements are

Private Type GUID_TYPE
                Data1 As Long
                Data2 As Integer
                Data3 As Integer
                Data4(7) As Byte
End Type
Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (Guid As GUID_TYPE) As LongPtr
Private Declare PtrSafe Function StringFromGUID2 Lib "ole32.dll" (Guid As GUID_TYPE, ByVal lpStrGuid As LongPtr, ByVal cbMax As Long) As LongPtr

Below is the actual function. Works like a charm

Function modIE_GetGUID()
On Error GoTo PROC_ERR
                Dim Guid As GUID_TYPE
                Dim strGuid As String
                Dim retValue As LongPtr
                Const guidLength As Long = 39 'registry GUID format with null terminator {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
                retValue = CoCreateGuid(Guid)
                If retValue = 0 Then
                                strGuid = String$(guidLength, vbNullChar)
                        retValue = StringFromGUID2(Guid, StrPtr(strGuid), guidLength)
                                If retValue = guidLength Then
                                                ' valid GUID as a string
            modIE_GetGUID = Mid$(strGuid, 2, 36)  ' removes the braces from the output
                                End If
                End If
PROC_EXIT:
            Exit Function
PROC_ERR:
            MsgBox "Error: " & Err.Number & ". " & Err.Description
            Resume PROC_EXIT
End Function

Recent Content