?
Solved

How to avoid duplicates when retrieving random records from a recordset

Posted on 2003-03-21
8
Medium Priority
?
233 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
[X]
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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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