Solved

Creating a random (non-duplicated) user id number.

Posted on 2002-03-14
5
172 Views
Last Modified: 2010-05-02
Is there a way whereby I can create a random ID number(of 5 or plus digits - numbers or letters) in VB. As long none of them are the same.

Is there an algorithm or function I can use? I don't know if Rnd can do the trick.

Thanks

Hannes


0
Comment
Question by:hannes1
5 Comments
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 200 total points
ID: 6863804
Hi,

Try this:

Dim myArray() As Long
Dim i As Long, j As Long

Private Sub Command1_Click()
    GetRandomID 100
End Sub

Private Sub GetRandomID(ByVal iRange As Long)
    Erase myArray
    Randomize
    For i = 0 To iRange - 1
        j = Format$(Int(Rnd * 100000), "00000")
        Debug.Print j
    Next i
End Sub
0
 

Expert Comment

by:pritam_dewan
ID: 6864428
i dont think u can really rely on generating unique random numbers.well u can try generating GUIDs.that will be unique globally.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6864481
sorry just not see carefully about "non-duplicated".

See this:
http://www.vbcode.com/asp/showsn.asp?theID=407

http://www.vbcode.com/asp/showsn.asp?theID=173
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6865302
Hi!

This is the code that I gave some time ago.  Chances are, it shouldn't be duplicated:

Dim myNumber As String, temp As Integer

For i = 0 To 9

Randomize
temp = Rnd * 10
If temp \ 2 = temp / 2 Then
myNumber = myNumber & Chr(48 + Int(Rnd * 10))
Else

Randomize
temp = Rnd * 10
If temp / 2 = temp \ 2 Then
myNumber = myNumber & Chr(65 + Rnd * 26)
Else
myNumber = myNumber & LCase(Chr(65 + Rnd * 26))
End If
End If

Next i

MsgBox myNumber


The thing is, using Randomize would make it more random.  

That's it!

glass cookie : )
0
 

Author Comment

by:hannes1
ID: 6876121
It works fine for me.

Thanks

Hannes
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question