Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2580
  • Last Modified:

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

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.
1 Solution
if you are not paticular about the length (8 chars), you can generate GUID which will be unique.
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)
   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

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.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now