Help with one recordset logic

Hi there. I am working on a feature in my study app where it will build a list of study sessions and disperse the articles for students to read on various dates. The total number of study sessions will differ as I am using a method to calculate the number of study days between today and the student’s test date, and including in a factor of the user choosing which days of the week he/she plans on studying. This part works and is here for reference

What I am trying to accomplish now is divvying up the articles of X number for each day of study and also accounting for the remainder of articles left. Let me paint a picture:

  • User selects 2021-12-01 as the test date and chooses 3 days per week to study
  • This equates to 28 study days until the student’s test date using the method in the link above
  • There are currently 334 articles in the database table. Using RowCount of 334 / NumberStudyDays of 28, this yields 11.9 topics per day
  • But 11 X 28 = 308 (short), and 12 X 28 = 336 (over)
  • For testing purposes, I am joining the IDs from the table in a string, and when they reach a threshold of TopicsPerDay, the string is added to an array, which later is used to display in a listbox

Here is a peek at the code. I know this is not best practices, and each string is beginning with a comma, but I’ll clean this up and deal with it later:

dim sessionIDs() as String
dim joinString as String
dim i as Integer = 0
while not rs.AfterLastRow
  joinString = joinString + "," + rs.Column("ID").StringValue
  i = i + 1
  if i = topicsPerDay then
    sessionIDs.Add joinString
    joinString = ""
    i = 0
  end if
  
  rs.MoveToNextRow
wend

for j as Integer = 0 to sessionIDs.LastIndex
  Listbox2.AddRow sessionIDs(j)
next

I know there is an issue with the if…then part in the loop since the last portion of IDs are not being added to the array since the threshold is not met. This is where I am stuck. Is there a way to loop through a RowSet in increments (11 or 12 in this example) without going out of scope? If so, is that the better way to handle this?

make a class with all the fields of the recordset (rowset in fact…)
copy all the recordsets in an array of the above class
then you have an array with your datas it’s easier to manipulate, and move inside.

1 Like

Thanks for the reply Jean-Yves, but it’s the remainder I am having a difficult time with. Let’s say for simplicity sake there are 10 total articles to be split up over the course of 4 days with 3 articles to study per day. The code I have above would have the threshold of 3 (if I = topicsPerDay (3) then…). Three days would get filled with the 3 topics each, and there is a remaining 1 topic to get added on the 4th day. How is that accomplished? I want the program to think “ok, I can fill each day with X number of topics, but when there’s not enough for the threshold, I need to fill the last day with the few remaining”. Does that make sense?

Maybe I just did not follow your suggestion if that was meant to help deal with the remainders

You might make an array of the number of articles per day. Initialize it to the minimum number of articles. Floor(334/28). Then spread the remaining articles over the array.

remain = 334 mod 28
for i = 1 to remain
   numArticles(i) = numArticles(i) + 1
next

That front-loads the extra articles. You could back-load them if you wish, so they study more articles per day just before the test.

Thanks Tim. Mod is what I was looking for to handle the remainders. This works, thank you!