Ok, first up, let’s translate that code to English.
Give me the number of seconds since the Unix “Epoch” time.
Just like VBA as a starting time/date, so does Unix and JavaScript uses the same standard.
That time is Jan 1, 1970.
Now this MIGHT not be important here – the code MIGHT just be using the number of seconds since then as a random type thing.
But, it DOES represent a date/time. So it is a VERY good idea to do the same thing!
Ok after we get the seconds, convert that into a hexadecimal string. Java is cute (2 is binary 16 is hex!!! - you get the idea!!!).
Then take those “xxxxxxxxxxxxx” (16) of them, and swap each X out with a RANDOM hexadecimal number!!! (0-f or 0-15 in decimal).
Then, convert the whole mess to lower case.
So 5F3BD is to become 5f3bd.
Ok, this code looks to create the same string with a matching first part.
I dont' know if that first part is imporant - but that posted code DOES MEAN that the first part is in fact datetime as HEX, so we better keep that part.
So, the function that returns the same?
This seems to do the trick:
Public Function mongoObjectId() As String
Dim lngT As Long
lngT = DateDiff("s", DateSerial(1970, 1, 1), Now())
Dim strT As String
strT = Hex(lngT)
Dim i As Integer
For i = 1 To 16
strT = strT & Hex(((Rnd() * 15)))
Next i
strT = LCase(strT)
mongoObjectId = strT
End Function
And thus Bob is your uncle!
Gee, I guess those days in Mom’s basement playing with that Apple II sometimes pays off! Oh how I remember those cute little hexadecimal days and writing 6502 assembler!
Regards,
Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada