Article ID: 170986 - Last Review: January 20, 2007 - Revision: 3.1 ACC: How to Fill a Table with Random Records from Another TableThis article was previously published under Q170986 Advanced: Requires expert coding, interoperability, and multiuser skills.
SUMMARY
This article demonstrates a method that you can use to fill a table with
random records from another table. The sample procedure in this article
uses Data Access Objects (DAO) to fill a table with a user-specified number
of records from an existing table. The following example randomly selects
records from the Orders table in the Northwind sample database and adds
them to a new table.
This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual. MORE INFORMATION
One way that you can get a certain number of random records from a table is
to create a query based on the table. You can create an expression that
applies the Rnd() function to a Number field in the table; if there is no
Number field available, you can apply the Len() function to a text field
and then apply the Rnd() function to the result. Next, you set the sort
order of this calculated field to ascending or descending, and then select
from the table the other fields that you want to see in the query result.
Finally, you set the query's TopValues property to the number of records
you want.
However, there is a disadvantage to this method. When you start Microsoft Access and run the query, a particular set of records is returned, which can be referred to as "Recordset A." You may run the query several more times with different results, and then quit Microsoft Access. The next time you start Microsoft Access and run the query, the result is again Recordset A. One resolution is to run a procedure that uses DAO to fill a table with a specified number of records from an existing table; this procedure also uses the Randomize statement to reinitialize the random-number generator. The following steps demonstrate how to use the sample subroutine BuildRandomTable.
REFERENCES
For more information about using a query to select random records, please
see the following article in the Microsoft Knowledge Base:
128874 (http://support.microsoft.com/kb/128874/EN-US/ ) : ACC: Find N Records in Random Order For more information about the Rnd function and the Randomize statement, search the Help Index for "random numbers." | Article Translations
|

Back to the top
