How do I assign random numbers to a table in Access 2002?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to assign random numbers to a table in access for a sweepstakes.
Anyone know how it can be done? Thanks!
 
I'm not sure about 2002, but the 2003 version offers Random as one of the
options for an autonumber. Another way would be the example below. It was
placed in the BeforeUpdate event of a form.

If Me.NewRecord Then
Randomize
'Make sure the number doesn't exist. If it does, try again.
Do
sglTestNumber = Rnd
Loop Until IsNull(DLookup("[Lottery Tie Breaker]", "tblBUMs", "[Lottery
Tie Breaker]=" & sglTestNumber))
Me.txtLotteryTieBreaker = sglTestNumber
End If

This will give a number >=0 and <1. If you want a different range of
numbers, that can be done also. The formula is:

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

This will produce random integers between the upper and lower bound.
 
Yes, ACCESS 2002 has the Random option for autonumber field in a table.

--

Ken Snell
<MS ACCESS MVP>

Wayne Morgan said:
I'm not sure about 2002, but the 2003 version offers Random as one of the
options for an autonumber. Another way would be the example below. It was
placed in the BeforeUpdate event of a form.

If Me.NewRecord Then
Randomize
'Make sure the number doesn't exist. If it does, try again.
Do
sglTestNumber = Rnd
Loop Until IsNull(DLookup("[Lottery Tie Breaker]", "tblBUMs", "[Lottery
Tie Breaker]=" & sglTestNumber))
Me.txtLotteryTieBreaker = sglTestNumber
End If

This will give a number >=0 and <1. If you want a different range of
numbers, that can be done also. The formula is:

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

This will produce random integers between the upper and lower bound.

--
Wayne Morgan
MS Access MVP


lschordock said:
I'm trying to assign random numbers to a table in access for a
sweepstakes.
Anyone know how it can be done? Thanks!
 
Random autonumbers have been around for a long time (they're in Access 97).
When did Replication get introduced? Random autonumbers are a requirement
for that.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Wayne Morgan said:
I'm not sure about 2002, but the 2003 version offers Random as one of the
options for an autonumber. Another way would be the example below. It was
placed in the BeforeUpdate event of a form.

If Me.NewRecord Then
Randomize
'Make sure the number doesn't exist. If it does, try again.
Do
sglTestNumber = Rnd
Loop Until IsNull(DLookup("[Lottery Tie Breaker]", "tblBUMs", "[Lottery
Tie Breaker]=" & sglTestNumber))
Me.txtLotteryTieBreaker = sglTestNumber
End If

This will give a number >=0 and <1. If you want a different range of
numbers, that can be done also. The formula is:

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

This will produce random integers between the upper and lower bound.

--
Wayne Morgan
MS Access MVP


lschordock said:
I'm trying to assign random numbers to a table in access for a
sweepstakes.
Anyone know how it can be done? Thanks!
 

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

Similar Threads

Generating a Random Number 4
How do I create a random sample from a table? 2
Auto numbering 1
Random Order of List 3
random assignment 0
Picking Random Numbers 3
Random Sampling 2
random number generation help 3

Back
Top