Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to avoid duplicates when retrieving random records from a recordset

Posted on 2003-03-21
8
Medium Priority
?
237 Views
Last Modified: 2012-06-21
Hi All

I am coding an application to display Multiple choice questions.
Having retrieved all questions of a certain area/category from the database i now want to display 10 random questions from that recordset (areaRS).

Generating the random questions is ok but I cant figure out how to stop duplicate questions being displayed.  At present, i store the question# of each question displayed in an array (usedNumbers) and then loop through it to compare the randomly generated question# with all the values in the array.
If it appears then it is a duplicate and therefore should not be displayed.- a new question then has to be generated and once again checked that its not a duplicate.

Maybe the logic is wrong but i cant think of any other way to solve this. - Maybe there is a better way to check for duplicates?

Heres the code i am working with:


'go to a random number in the recordset
Randomize
randNum= Int(Rnd*rsNumber)+1
areaRS.Move randNum, adBookmarkFirst

'Check that its not the BOF or EOF
if areaRS.BOF = true then
areaRS.MoveNext
End If

if areaRS.EOF = true then
areaRS.MoveFirst
End If

questionToCheck= areaRS("Question#")

usedNumbers(arraycounter)=areaRS("Question#")

'If question has already been used then generate a new one
IF usedNumberChecker(questionToCheck)=true then
DO WHILE usedNumberChecker(questionToCheck)=true
randNum= Int(Rnd*rsNumber)+1
areaRS.Move randNum, adBookmarkFirst
questionToCheck=areaRS("Question#")
LOOP
END IF

'Display the question code is in here


FUNCTION usedNumberChecker (quest)
for y= 0 to no_of_posed   ' no_of_posed is a variable holding the number of questions already posed
IF usedNumbers(y)=quest Then '***** This line of code causes errors quite often for some reason
usedNumberChecker=True
END IF
Next
END FUNCTION

I keep getting this error message:
ADODB.Field error '800a0bcd'
Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.


Any suggestions are greatly appreciated.
Thanks in advance
Aljo

0
Comment
Question by:Aljo
8 Comments
 
LVL 3

Expert Comment

by:keystrokes
ID: 8181257
Do you have a primary key like an ID field in the table?  The way I would do it is generate a Random list and store in an array, make sure there is no dup before you store them.  Then you run a query that match this set of criteria, this way you don't have to move the cursor back and forth and the query only returns a small recordset.
0
 

Author Comment

by:Aljo
ID: 8181362
Question# is a primary key.  Im not sure i quite understand.Is this right?- Generate 10 random question numbers then use SQL to retrieve  a recordset consisting of only those ten questions.- Would the SQL not be very complicated with 10 or conditions and would a loop have to be used?  

Thanks
Aljo
0
 
LVL 4

Expert Comment

by:mberumen
ID: 8182049
to elaborate on keystrokes suggestion.

you would generate 10 random numbers making sure they are not duplicates and that they fall between the first and last id in your table.

ie. (34,5,76,28,15,2,4,43,85,77)

now you can query your table with the IN operator

"Select * from questions where id in (34,5,76,28,15,2,4,43,85,77);"

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 8182420
here is your code modified to work perfectly



for i=1 to 10
'go to a random number in the recordset
Randomize
randNum= Int(Rnd*rsNumber)+1
areaRS.Move randNum, adBookmarkFirst

'Check that its not the BOF or EOF
if areaRS.BOF = true then
areaRS.MoveNext
End If

if areaRS.EOF = true then
areaRS.MoveFirst
End If

questionToCheck= areaRS("Question#")

usedNumbers(i)=areaRS("Question#")

'If question has already been used then generate a new one
IF usedNumberChecker(questionToCheck)=true then

        WHILE usedNumberChecker(questionToCheck)=true
           randNum= Int(Rnd*rsNumber)+1
           if RS.EOF OR RS.BOF then
                'this is where the error is coming from
           else          
                 areaRS.Move randNum, adBookmarkFirst
                 questionToCheck=areaRS("Question#")
           end if
        WEND
END IF

'Display the question code is in here

NEXT

FUNCTION usedNumberChecker (quest)
for y= 0 to no_of_posed   ' no_of_posed is a variable holding the number of questions already posed
IF usedNumbers(y)=quest Then '***** This line of code causes errors quite often for some reason
usedNumberChecker=True
END IF
Next
END FUNCTION


let me know how that goes...

MaxOvrdrv2
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 8182448
sorry... i made an error in the placement in the code above... in the "checking for duplicates" loop/part... here is what it should look like:

WHILE usedNumberChecker(questionToCheck)=true
          randNum= Int(Rnd*rsNumber)+1
          areaRS.Move randNum, adBookmarkFirst
          if RS.EOF OR RS.BOF then
               areaRS.MoveFirst   'this is where the error comes from
          else          
                questionToCheck=areaRS("Question#")
          end if
WEND

sorry about that... make sure to use this loop instead or you will be getting the same error...

sorry again...

MaxOvrdrv2
0
 
LVL 10

Accepted Solution

by:
MaxOvrdrv2 earned 460 total points
ID: 8182525
OMG... i've made another error in my post... really sorry... here is the proper code:

WHILE usedNumberChecker(questionToCheck)=true
         randNum= Int(Rnd*rsNumber)+1
         areaRS.Move randNum, adBookmarkFirst
         if RS.EOF OR RS.BOF then
              areaRS.MoveFirst   'this is where the error comes from
         end if
         questionToCheck=areaRS("Question#")
WEND


MaxOvrdrv2
0
 

Author Comment

by:Aljo
ID: 8184035
MaxOvrdrv2,

That has solved it.  Thanks for your help.

Aljo
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 8195105
glad i could help! sorry i made so many errors! :-) i was very tired that day! ;-)

MaxOvrdrv2
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

579 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