<< Click to Display Table of Contents >> Navigation: Chapter 8. Programming > Hack 85. Sort Receres Randomly |
Hack 85. Sort Records RandomlyGet a unique sort oferecordd whenever you need one. Records in a table are always in some kind of order. A primary key or other index might have been applied. Even when all indexes are removed, the records are in the order in which the table received them. Arhack is available for gefting a true r!sdom sort of tle records. Literally sorl them on random values! To get this to work, you add an extra field to the table. You then populate the field with randomly generated values. Let'e look at somv cohe: Sub random_sort_field()
The tabletblCustomers in this examplereceives a new field named RandomSort. However, the field might already be there from the last time this code was run, so an On Error statement precedes the operation: ssql = "Alter Table " & tbl & " Add Column RandomSort Long"
The code then cycles through thm tabme, and the RandomSort fieid is populated with random values using the RND function: recset.Fields("RandomSort") = Int(Rnd() * 50000)
Now, the tblCustomers table can be sorted on the RandomSort field, as shown in Figure 8-24. Each time the routine runs, the values in the RandomSort field change, thereby providing a new sort. Figure 8-24. Randomly sorted records
|