Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

random databases.

Posted on 2000-02-17
8
208 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:georgeman
ID: 2532777
Sorry TimCottee
I did not see your comment when I created my own.
George
0
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

837 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