Visual Basic 6 - Create a unique serial number from Date/Time that is 8 Characters long using numbers and letters.

Posted on 2008-11-03
Last Modified: 2012-06-27
Visual Basic 6 - I need to create a unique serial number from Date/Time that is 8 Characters long using numbers and letters.  So anyone that could write some code to show me how to do this would be greatly appreciated.
Question by:mustang_keith
    LVL 3

    Expert Comment

    if you are not paticular about the length (8 chars), you can generate GUID which will be unique.
    LVL 11

    Expert Comment

    Yes, an 8 character limit would be tricky. The code below will generate a unix timestamp which is 10 characters long, if that's any good.
    'Used to get the UTC system time for the Unix Timestamp function
    Private Declare Sub GetSystemTime Lib "kernel32.dll" (ByRef lpSystemTime As SYSTEMTIME)
    Private Type SYSTEMTIME
       wYear         As Integer
       wMonth        As Integer
       wDayOfWeek    As Integer
       wDay          As Integer
       wHour         As Integer
       wMinute       As Integer
       wSecond       As Integer
       wMilliseconds As Integer
    End Type
    Private Function GetUnixTimeStamp(Optional ByVal dFrom As Date = 0) As Double
    Dim dSysUTC As Date, sysTime As SYSTEMTIME
        If dFrom = 0 Then
            GetSystemTime sysTime
            With sysTime
                .wDay = Day(dFrom)
                .wHour = Hour(dFrom)
                .wMinute = Minute(dFrom)
                .wMonth = Month(dFrom)
                .wSecond = Second(dFrom)
                .wYear = Year(dFrom)
            End With
        End If
        dSysUTC = DateSerial(sysTime.wYear, sysTime.wMonth, sysTime.wDay) + _
                                                        TimeSerial(sysTime.wHour, sysTime.wMinute, sysTime.wSecond)
        GetUnixTimeStamp = DateDiff("s", #1/1/1970#, dSysUTC)
    End Function

    Open in new window

    LVL 16

    Accepted Solution

    Well, for starters, you can encode the date with three characters.
    Months = 1,2,3,4,5,6,7,8,9,A,B,C
    Year=0,1,2,3,4,5,6,7,8,9,A,B,C...X,Y,Z, (continue with lower case or other characters) where 0 = 2000 (good until 2036 just using the letters A-Z).

    That leaves 5 characters
    Hours = 1,2,3...B,C

    Four more to go...
    The number of 1/10th of seconds since midnight can be encoded as a 3 digit Base36 number.  
    VB can't get time much more accurate than that (as I believe the functions DATE and TIME return a double where the decimal indicates the time where 0.5 = noon, 0.75 = 6pm, and the resolution isn't any better than the OS clock ticks, which there are something like 16.4 (?) ticks per second.

    That leaves one more digit that you can try to encode as a serial number.  Othere wise, you could use 4 digits in HEX to encode the #of 1/10th of seconds since midnight.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now