Solved

random databases.

Posted on 2000-02-17
8
193 Views
Last Modified: 2010-05-02
I've got a relatively small database sorted by number.

How can I sort this to display these entries in a random order, each time I run the program, and not repeat entries?
0
Comment
Question by:SRP
  • 3
  • 3
  • 2
8 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 2532715
You could add an extra field to the recordset, then fill this with a random number and order it on this.

rstRandom.Open "SELECT Number,Field1,Field2...Field3,Random = 0.0 FROM TableName",cnnDatabase,adOpenStatic,adLockOptimistic
With rstRandom
  .MoveFirst
  Do
    !Random = Rnd() * 100
    .MoveNext
  Loop Until .Eof
End With
rstRandom.Sort = "Random"

Or something like that anyway.
0
 
LVL 1

Accepted Solution

by:
georgeman earned 50 total points
ID: 2532754
Try this algorythm:

- load all db records in rs recordset

- get number records in recordset
  rs.MoveLast
  NumberRecords = rs.RecordCount

- create new empty recordset newrs

- create array(NumberRecords) as integer (or long if you have big database)

- for i=1 to NumberRecords
     'get random number
 repeat1:
     RandomNumber = Rnd()
    'if RandomNumber doesn't exist in array - add it to array
    'else goto Repeat1

     'get record number=RandomNumber from rs recordset
     rs.findfirst "number = " & RandomNumber
    'upload record from rs to newrs
  next i

- newrs is now copy of rs but organised in random order.

Regards
George
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 2532767
That would work as well.
0
 
LVL 1

Expert Comment

by:georgeman
ID: 2532777
Sorry TimCottee
I did not see your comment when I created my own.
George
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 43

Expert Comment

by:TimCottee
ID: 2532787
That is OK, you used a different method though the thought process is obviously similar.
0
 

Author Comment

by:SRP
ID: 2537985
This is a v. good answer, and a very simple. But one brief question, will it produce a different random order each time the program runs?

OK cheers.
SAM
0
 
LVL 1

Expert Comment

by:georgeman
ID: 2540481
It depands how you will organise the process of creating of random numbers. One tip: you can use for that current date/time info so random numbers will be really random because these values are unique for every time when you start your program (or when you start this process inside program).
George
0
 

Author Comment

by:SRP
ID: 2543663
Cheers, I thought I had already accepted the answer. Obviously not!

Here are the points.

Thanks
SAM
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now