You are here:

Using MS Access/Generate a random unique number

Advertisement


Question
Hi All,

I'm designing this database, in which, I have to give each record a unique random (or sequance) number of 5 digits, giving that the first digit must be retrived from another field.

Please Help, I need this ASAP!

Any help will be very much appreciated!

Thanks,
CS.  

Answer
The expression:

=Int((100000 * Rnd) + 1)

Will generate a random number, usually of 5 digits. In my testing, It sometimes came up with less, though. So you can use this function to insure that its 5 digits

Public Function Random5() As Long
Do While Random5 < 10000
Random5 = Int((100000 * Rnd) + 1)
Loop

End Function

I just reread your request. If you are saying that the first of the 5 must be gotten from another field so that if the other field contains a 3 then the number should be 31234, then you need to change the function a bit.

Public Function Random5(intFirst As Long) As Long
Do While Random5 < 1000
Random5 = Int((10000 * Rnd) + 1)
Loop
Random5 = intFirst * 10000 + Random5

End Function

To retrieve the number use:
=Random5([fieldname])



HTH
Scott<>

Using MS Access

All Answers


Answers by Expert:


Ask Experts

Volunteer


Scottgem

Expertise

I can answer almost all types of questions relating to Microsoft Access usage and application design. My strengths are database and interface design.

Experience

I've been designing databases for over 15 years working with dBase, FoxPro, Approach and Access.

Organizations
Author of Microsoft Office Access 2007 VBA
Techncial Editor for Special Edition Using Microsoft Access 2007 and Access 2007 Forms, Reports & Queries From Que Publishing

©2012 About.com, a part of The New York Times Company. All rights reserved.