Randon Query HELP

D

Di

I have a table with 50,000 names and need to run a query that will
give me 10,000 randomly picked names (this will change periodically).
I have seen this code in some Access group, but need more specifics:

Public Function RndNum(vIgnore As Variant) As Double
Static bRnd As Boolean
If Not bRnd Then
'Initialize the random number generator once only
bRnd = True
Randomize
End If
RndNum = Rnd()
End Function

Since I'm a newbie at this, I was hoping someone could offer me a more
step-by-step type of assistance. Keep in mind that I'm not a
programmer, just a gal trying learn something new for her job. Any
help on how I can accomplish this task would be greatly appreciated.

Thank you so much,
Diane
 
D

Dirk Goldgar

Di said:
I have a table with 50,000 names and need to run a query that will
give me 10,000 randomly picked names (this will change periodically).
I have seen this code in some Access group, but need more specifics:

Public Function RndNum(vIgnore As Variant) As Double
Static bRnd As Boolean
If Not bRnd Then
'Initialize the random number generator once only
bRnd = True
Randomize
End If
RndNum = Rnd()
End Function

Since I'm a newbie at this, I was hoping someone could offer me a more
step-by-step type of assistance. Keep in mind that I'm not a
programmer, just a gal trying learn something new for her job. Any
help on how I can accomplish this task would be greatly appreciated.

Thank you so much,
Diane

The function is designed to be called from a query, and returns a new
random number each time it is called. The first time it is called, it
executes the Randomize to re-seed the random-number generator so that
you don't get the same set of random numbers each time you run it.

Copy the function and paste it into a standard module. If you create a
new module for the purpose, don't name it "RndNum" -- it mustn't have
the same name as any public function. You can use the default name,
"Module1" (or whatever), or you can pick a name of your own, like
"modUtilities". Save the module.

To use this to pick 10,000 random records, you create a calculated field
in the query that calls this function, passing it some field from the
table. It doesn't matter which field you pick. If your table has a
field named "ID", for example, you might define a calculate field

PickRandom: RndNum([ID])

Set the query to sort by this calculated field, and set the query's Top
Values property to 10000. Now the query will return 10,000 records
chosen at random from the table.
 

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