Problems with Random Numbers

crhodus

Registered User.
Local time
Today, 15:26
Joined
Mar 16, 2001
Messages
257
On one of my forms, I have a field named CallID that is the primary key for my table.

For the default value, I have the following code:
=[Forms]![1_Company_Form]![COMPANY_NUMBER] & Rnd()

My code for generating a random number has been working great until now. For some reason it has been generating the same random number continuiously. Because of this, a new record can't be saved because this primary key already exists in the database.

Is it possible to be generating the same number 5 or 6 times in a row? I closed down access and restarted the program, but it still did the same thing.
 
Random, as you have found out, will not always give you a unique number. The best number to use for a Primary key is Autonumber. I do not know what to tell you to do if you want to continue using your current method but I would seriously consider a different approach as soon as possible...
 
Random generates numbers seeded from the current date - that's how you get the same number so many times - you need to call Randomize in order for it to get going properly. Jacks autonumber is the best solution unless you really do need a random number, in which case you'll have to generate it in VBA,

Drew
 
I ended up combining the random number with the current data and time (hour minute seconds). Do you see any problem that I could run into by doing this?

I can't use autonumber because 8 different people will have separate databases when they are out on the field. At the end of each week, they submitt their data into one large database.
 
Not unless you/they add 2 records in the same second. But then why not drop the rnd and just use the dt string alone? - you could always format it to look random,

Drew
 
ok...so I don't know the code, but I am sure someone does...

Couldn't you have it generate a random number, then check the field the number is to go in, if there exists a duplicate number, REGENERATE the number and check again...?

Im sorry I don't know the code...wish I did, but I have seen something similar to this in a visual basic class in college that I took for for a friend...
(never learned a damn thing...stupid instructors teach so that as long as you type what they say you pass...ugh!)

But anyway...
Hope that helps...
 

Users who are viewing this thread

Back
Top Bottom