draw random samples

S

Stephani

I have a database of over 200 records in ACCESS. I need to audit 30 records
at random. Is there a program that can help me with that tasks.
 
A

Allen Browne

If your table has an AutoNumber primary key field named ID, you could do it
with this query:
SELECT TOP 30 * FROM MyTable ORDER BY Rnd([ID]), ID;

Before you run the query, open the Immediate Window (Ctrl+G), and enter:
Randomize

How it works
==========
The Rnd() function generates a random number each time it's called. The
query optimiser doesn't bother calling the function on every row, unless you
pass in a field, so that's important even though Rnd() doesn't do anything
with the field.

This gives you random sorting in your query, and you wanted 30 results, so
we use TOP to select the first 30 from the random sort. Just in case Rnd()
should ever create 2 identical results, we add the primary key to the ORDER
BY clause, so Access can distinguish between them, and return exactly 30
records. (If there are multiple matches, it returns the ties as well.)
 

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