How to check if number was already used

G

Guest

I have a piece of code listed below that will generate a random number
between 1-100000. I know that it is slim that there will be a match but I
still have to check.

I need to check that the new random number before I add it to the table to
see it is the same as a value already in the table. Basicly I can't have two
of the same random numbers in the table. I need some help with the code that
will check this for me.

Genetic_ID_number is the table where the information needs to go and to
check if there is a duplicate

geneticIDnum is the field which has the random numbers in it
Thanks for the help.


Randomize
RandomNum = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

lblRandom.Caption = RandomNum

strSQL = "INSERT INTO Genetic_ID_number (geneticIDnum) " & "VALUES (" &
RandomNum & ");"
CurrentDb.Execute strSQL
 
D

Dan Artuso

Did you actually try my reply in your other post?
Please try and keep to the same thread you started so that
people's efforts are not duplicated.

Try the DCount solution that I posted in the other thread.
 
R

RW

Try this?

Randomize
RandomNum = Int((upperbound - lowerbound + 1) * Rnd +
lowerbound)
If Dcount
("[geneticIDnum]", "[Genetic_ID_number]", "[geneticIDnum]
=" & RandomNum)= 0 Then
lblRandom.Caption = RandomNum
strSQL = "INSERT INTO Genetic_ID_number
(geneticIDnum) " & "VALUES (" & RandomNum & ");"
CurrentDb.Execute strSQL
EndIf

RW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top