Link to home
Start Free TrialLog in
Avatar of Aljo
Aljo

asked on

How to avoid duplicates when retrieving random records from a recordset

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

Avatar of keystrokes
keystrokes
Flag of United States of America image

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.
Avatar of Aljo
Aljo

ASKER

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
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);"

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
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
ASKER CERTIFIED SOLUTION
Avatar of MaxOvrdrv2
MaxOvrdrv2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aljo

ASKER

MaxOvrdrv2,

That has solved it.  Thanks for your help.

Aljo
glad i could help! sorry i made so many errors! :-) i was very tired that day! ;-)

MaxOvrdrv2