Article ID: 210468 - Last Review: October 11, 2006 - Revision: 2.2 ACC2000: Sample Function to Return a Random Record from a FieldThis article was previously published under Q210468 This article applies only to a Microsoft Access database (.mdb).
Moderate: Requires basic macro, coding, and interoperability skills. SUMMARY
Microsoft Access does not have a built-in mechanism for returning a
random record from a set of records. This article describes a sample
user-defined function that you can use to return a random record.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. MORE INFORMATION
The following sample function will return a random record using the
recordset name and the field name that you provide.
NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected. Create a module and type the following line in the Declarations section if it is not already there:
?FindRandom("<RecordSetName>", "<FieldName>")
where <RecordSetName> is the name of a table or query or a SQL statement and <FieldName> is the name of a field in your recordset.
Note that each time that you run the function, a different record is returned. REFERENCES
For additional information about using a query to return random records, please see the following articles in the Microsoft Knowledge Base: 208855
(http://support.microsoft.com/kb/208855/EN-US/
)
ACC2000: Find N Records in Random Order
For more information about <B<Int() function or the <Bnd() function, click Microsoft Access 2000 Help on the
Help menu, type Int or Rnd in the Office Assistant or
the Answer Wizard, and then click Search to view the topics
returned.
| Article Translations
|
Back to the top
