[COLOR="Navy"]Option Explicit[/COLOR]
[COLOR="Navy"]Private[/COLOR] [COLOR="Navy"]Declare[/COLOR] [COLOR="Navy"]Function[/COLOR] Sleep [COLOR="Navy"]Lib[/COLOR] "kernel32" (ByVal dwMilliseconds [COLOR="Navy"]As[/COLOR] Long) [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Private[/COLOR] mbln_TEST_PROC [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Boolean[/COLOR]
[COLOR="Navy"]Private[/COLOR] [COLOR="Navy"]Function[/COLOR] Random_Key() [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Static[/COLOR] d [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Date[/COLOR]
[COLOR="Navy"]Dim[/COLOR] a [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] b [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="SeaGreen"]''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[/COLOR]
[COLOR="SeaGreen"]'To create a random number based on the date (ignoring year)[/COLOR]
[COLOR="SeaGreen"]'Example: April 25 @ 2:20 PM and 45 seconds[/COLOR]
[COLOR="SeaGreen"]' result --> "275020451"[/COLOR]
[COLOR="SeaGreen"]' [2750] hours passed this year...[/COLOR]
[COLOR="SeaGreen"]' [2045] at the twentieth minute and 45th second...[/COLOR]
[COLOR="SeaGreen"]'Keys will repeat in successive years[/COLOR]
[COLOR="SeaGreen"]'Notes:[/COLOR]
[COLOR="SeaGreen"]' * Leading zeros in Jan/Feb could be stripped off if value is used as a number (numbers will still be unique)[/COLOR]
[COLOR="SeaGreen"]' * Add a year prefix to maintain uniqueness across years:[/COLOR]
[COLOR="SeaGreen"]' Rand_Key = Format(d,"yyyy") & format(a,"0000") & b[/COLOR]
[COLOR="SeaGreen"]' * Add a static incrementer prefix/postfix to maintain uniqueness across faster successive calls (without using Sleep)[/COLOR]
[COLOR="SeaGreen"]' Rand_Key = Format(d,"yyyy") & format(a,"0000") & b & myCounter[/COLOR]
[COLOR="SeaGreen"]''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[/COLOR]
[COLOR="Navy"]Do[/COLOR] [COLOR="Navy"]While[/COLOR] (Now - d) < (1 / 86400) [COLOR="SeaGreen"]'//wait at least 1 second between function calls[/COLOR]
Sleep 100
[COLOR="Navy"]Loop[/COLOR]
d = Now
[COLOR="SeaGreen"]'TEST CODE ------------[/COLOR]
[COLOR="Navy"]If[/COLOR] mbln_TEST_PROC [COLOR="Navy"]Then[/COLOR]
d = Get_Test_Date()
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="SeaGreen"]'----------------------[/COLOR]
a = (Val(Format(d - 1, "y")) * 24) + (Val(Format(d, "hh"))) [COLOR="SeaGreen"]'Num of hours in this year to the present hour[/COLOR]
b = Format(d, "nnss") [COLOR="SeaGreen"]'Minute and second of this day[/COLOR]
Random_Key = Format(a, "0000") & b
[COLOR="SeaGreen"]'TEST CODE ------------[/COLOR]
[COLOR="Navy"]If[/COLOR] mbln_TEST_PROC [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]Debug[/COLOR].[COLOR="Navy"]Print[/COLOR] Format(d, "yyyy-mm-dd hh:nn:ss") & " Random Key: " & Random_Key
d = 0
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="SeaGreen"]'----------------------[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Function[/COLOR]
[COLOR="Navy"]Sub[/COLOR] Test_It()
[COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
mbln_TEST_PROC = False
Randomize
[COLOR="Navy"]For[/COLOR] i = 1 [COLOR="Navy"]To[/COLOR] 50
[COLOR="Navy"]Call[/COLOR] Random_Key
[COLOR="Navy"]Next[/COLOR] i
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]Function[/COLOR] Get_Test_Date() [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Date[/COLOR]
[COLOR="Navy"]Static[/COLOR] d [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Date[/COLOR]
[COLOR="Navy"]If[/COLOR] Year(d) > Year(Date) [COLOR="Navy"]Or[/COLOR] d = 0 [COLOR="Navy"]Then[/COLOR]
d = DateSerial(Year(Date), 1, 1)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
d = d + (Rnd() * (360 / 15))
Get_Test_Date = d
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Function[/COLOR]