VBA function that generates a MS-style serial number?

Sorry for having to ask this, but a few years ago I came across a VBA function that generates a random string that looks a lot like a typical MS software key, for example: H2F6F-W3RYD-AHDFYKV2PM-RBMBW

I have looked through my stuff and googled, and all I can find is about RND() and people's various hand-written code.

Can somebody please tell me the name of the VBA function that does this?

Mark LaGrangeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertConsultingCommented:

These numbers are also called Global Unique Identifiers (GUID), and to my knowledge, there is no built-in function returning such values.
Are you working with MS Access or MS Excel ?

With MS Access, it is fairly easy:
Create a table, with a unique column, named ID.
Set the column's to auto-number, data type: Replication ID.
Save the table as GUID (hide it if you want).

Write a function that will:
Open a dynamic recordset on the GUID table.
Add a new record.
Return the ID field's value.
Cancel the update.

See the attached database for détails.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fabrice LambertConsultingCommented:
I also found this on MSDN, using Windows API:
Option Explicit

    ' No VT_GUID available so must declare type GUID
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

Public Function CreateGuidString() As String
    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
            CreateGuidString = strGuid
        End If
    End If
End Function

Open in new window

Mark LaGrangeAuthor Commented:
Thanks - your mention of "GUID" jogged my memory enough for me to go find the code I had used before:
strTmpARNmbr = Mid(CreateObject("Scriptlet.TypeLib").GUID, 2, 8)
(For that particular use, I was in Excel VBA, and only needed a portion of the whole string returned)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.