Will somebody please help a newbie create unique numbers in VB6, hopefully using DateTime stripped of non-numeric characters?
Any help gratefully accepted.
Printable View
Will somebody please help a newbie create unique numbers in VB6, hopefully using DateTime stripped of non-numeric characters?
Any help gratefully accepted.
Do you mean stripping a Date-Time Var clean of non-num chars?
Code:Function Strip(DateTime as Date) As Long
Dim s, i, t, ss
s = Str(DateTime)
For i = 1 To Len(s)
t = Mid(s, i, 1)
If Not IsNumeric(t) then t = ""
ss = ss + t
Next
Strip = Int(ss)
End Function
Or to create random(unique) numbers:
Gl,Code:Text1.Text = Int(Rnd * 1000)
D!m
Try this code: It creates a GLOBAL UNIQUE IDENTIFIER (which should be always unique ... in the world(!) )...
Create a new project and add a command-button and a Label
Paste this code
Enjoy!Code:Option Explicit
Private Type GUIDType
D1 As Long
D2 As Integer
D3 As Integer
D4(8) As Byte
End Type
Private Declare Function WinCoCreateGuid Lib "OLE32.DLL" Alias "CoCreateGuid" (g As GUIDType) As Long
Public Function CreateGUIDString() As String
Dim g As GUIDType
Dim sBuf As String
Call WinCoCreateGuid(g)
sBuf = _
PadZeros(Hex$(g.D1), 8, True) & _
PadZeros(Hex$(g.D2), 4, True) & _
PadZeros(Hex$(g.D3), 4, True) & _
PadZeros(Hex$(g.D4(0)), 2) & _
PadZeros(Hex$(g.D4(1)), 2, True) & _
PadZeros(Hex$(g.D4(2)), 2) & _
PadZeros(Hex$(g.D4(3)), 2) & _
PadZeros(Hex$(g.D4(4)), 2) & _
PadZeros(Hex$(g.D4(5)), 2) & _
PadZeros(Hex$(g.D4(6)), 2) & _
PadZeros(Hex$(g.D4(7)), 2)
CreateGUIDString = sBuf
End Function
Private Function PadZeros(ByVal sBit As String, ByVal iStrLen As Integer, Optional bHyphen As Boolean) As String
If iStrLen > Len(sBit) Then
sBit = Right$(String$((iStrLen - Len(sBit)), "0") & sBit, iStrLen)
End If
If bHyphen Then sBit = sBit & "-"
PadZeros = sBit
End Function
Private Sub Command1_Click()
Label1.Caption = CreateGUIDString
End Sub
For trouble ticket systems I wrote in ASP once upon a time I used
round(Cdbl(NOW()) * 100000,0)
so you could trace the ticket back to the time it was created.
or if user load is high then multiply by 10,000,000 so you get 1000 per second rather than 10
Guys.
He told me, by e-mail, that he had already solved the problem. He does mean what I thought, and He used Format(now, "MMddyyhhmmss") and solved it. We should stop trying to overcrowd VB-World [JOKE].
He solved it, so it saves energy to stop:).