Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating Unique number 5

Status
Not open for further replies.

savok

Technical User
Jan 11, 2001
303
AT
How would i go about generating a unique number to use as an ID?

 
Try AutoNumber. If this does not meet your needs, look at faq700-184 It is somewhat more elaborate, and illustrates how you can 'customize' the value to include additional information.


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
To create a GUID (Global Unique Identifier): [tt]

Private Declare Function CoCreateGuid Lib "ole32.dll" (firstbyte As Byte) As Long
Private Declare Function StringFromGUID2 Lib "ole32.dll" (firstbyte As Byte, ByVal lpsz As Long, ByVal cbMax As Long) As Long

Private Sub Form_Load()
Dim guidbuffer(15) As Byte
Dim guidstring As String
Dim hres As Long
Dim chars As Long

guidstring = String$(128, 0)
hres = CoCreateGuid(guidbuffer(0))
chars = StringFromGUID2(guidbuffer(0), StrPtr(guidstring), 128)
MsgBox Left$(guidstring, chars)

End Sub
[/tt]

VCA.gif

Alt255@Vorpalcom.Intranets.com

"The bottom line is that almost all software ships with significant bugs and performance limitations."
Michael Spertus in Software Development Times
 
Alt255 has the best option here . . . GUID generation will always generate a unique number for you. - Jeff Marler
(please note, that the page is under construction)
 
GUID's are also unique across different machines, so if your clients are on a network, you're always going to get a unique number.

Only downside is that they're big (8 bytes).

Chip H.
 
Actually, according to MS. GUID's are ALNOST never duplicated, while the custom generation CAN be GUARNTEED - depends on the programmer's skill, dedication ...



MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
MichaelRed,
Where did you see that MS stated that GUIDs could be duplicated (please post a link if you have one). I was under the impression that MS took the position that GUIDs would never be repeated. - Jeff Marler
(please note, that the page is under construction)
 
Actually, I am NOT certain of where. I believe that it was in Ms. Access ver '97 Help. They also stated the 'probability' of duplication, and it was a VERY samll number - but it was NOT zero. Since Ms. (or was it me?) completly RUINED the Ms. Access Help system w/ 2K, I cannot find most of the previous help. Oh, well, so it goes.


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
I 'tried' to get to the GUID help in MS. Access 2K. It 'threw' me into "replication Id" as fast as any 'little old lady' could possibly move. There, it says that Replication Id's are 128 Bytes. So, I guess I'm even further afield. I would THINK that 128 Bytes would be sufficient to generate a a Unique Id for every molecule and every datum in the entire known universe - on the other hand unless the Id Generator is alos universal, the possability for duplication cannot be zero.


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Last I heard, the GUID generation algorithm uses a few pieces of information that help to make it unique even across time and loation.
One piece that it uses is the time/date that the GUID deneration function was called. That way, if you call it again, then you will get a different number.
Another piece that it uses is the MAC address of your network card (if you have one) and/or the serial numbers of the motherboard and/or the CPU. This allows the GUID to be unique to a specific computer. Wanna test this? If you have a NIC card, then go ahead and generate a GUID (you can do this by compiling a quick VB ActiveX DLL) and look up the GUID in the registry.


{XXXXXXXX-XXXX-XXXX-XXXX-************}



The last part (last 12 digits) of the GUID is your MAC address. And, as a side note, allows people to track what computer a GUID was generated on AND, consequently, what computer compiled an ActiveX component.

That is the info that I have on the matter and that is why GUID generation does not need to be universal or talk to some global server to verify that it is unique. - Jeff Marler
(please note, that the page is under construction)
 
Alt255 the id that was generated was
{ED8C31D8-1A30-11D5-BF4E-00508BACEE9D}
when i try to insert that into a table it gives me an error saying "quoted string not properly terminated"

heres the code

DBComm.CommandText = "Insert into class(id) values('" & strID & "')"
DBComm.CommandType = adCmdText
DBComm.Execute

 
Which type of AutoNumber field should I create?

Microsoft Access provides the AutoNumber data type to create fields that automatically enter a number when a record is added. Once a number is generated for a record, it can't be deleted or changed. An AutoNumber field can generate three kinds of numbers: sequential numbers that increment by one, random numbers, and Replication ID (also referred to as GUIDS — globally unique identifiers) numbers. AutoNumbers that increment by one are the most common kind of AutoNumber and are a good choice for use as a table's primary key. Random AutoNumbers will generate a random number that is unique to each record within the table. Replication ID AutoNumbers are used in database replication to generate unique identifiers for synchronizing replicas. Learn more about Replication ID AutoNumbers and when to use them.


A Guid autonumber is for Replica's !!


Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
Dim guidbuffer(15) As Byte
Dim guidstring As String
Dim hres As Long
Dim chars As Long
guidstring = String$(128, 0)
hres = CoCreateGuid(guidbuffer(0))
chars = StringFromGUID2(guidbuffer(0), StrPtr(guidstring), 128)
strUniqueID = Left$(guidstring, chars)

if i then try to insert strUniqueID into the table it says "the quoted string not properly terminated"

but if i insert the value directly it works fine
strUniqueID = "{ED8C31D8-1A30-11D5-BF4E-00508BACEE9D}"

thanks
 
Savok, try:

strUniqueID = Left$(guidstring, chars -1 )

strUniqueID is terminated with a null character.
VCA.gif

Alt255@Vorpalcom.Intranets.com

"The bottom line is that almost all software ships with significant bugs and performance limitations."
Michael Spertus in Software Development Times
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top