Assign Random Variables Without Duplicates
I have a pretty tough VBA question, and I am hoping someone could point me in the right direction to fix my code. Right now I am attempting to create a generic scheduling program, and the setup I am using is a school scheduling program. The goal is to take teachers and assign the classes they are going to teach to periods of the school day. And the code works great except for one thing: the code runs through and puts a teacher teaching multiple classes during the same period.
What I have in my code is a period (1 through 8) is assigned randomly to a class/teacher, then the next record is handled and assigned a random period. What I want the code to do is compare a string value, composed of the teacher ID and period ID for any duplicates - if there are duplicates, this means that the teacher is being assigned multiple classes the same period. And when this occurs, I want the code to re-run and assign a new random period to that record until there are no duplicates.
Below is the code that I have. If I remove the portion that say "Experiemental Code", it will assign all classes, but there are duplicate periods assigned. I can also get it to loop, but I get stuck in an infinite loop on the third record. Any help fixing or providing me with suggestions for the "Experiemental Code" section would be great!
Set rst = db.OpenRecordset("tblTempMasterSchedule", dbOpenTable)
rst.MoveFirst
rst.Edit
rst![PeriodID] = Int((8 - 1 + 1) * Rnd() + 1)
rst.Update
rst.MoveNext
Do
rst.Edit
rst![PeriodID] = Int((8 - 1 + 1) * Rnd() + 1)
rst.Update
'==========================================================
'Experimental Code
'Needs to check to make sure TeacherID/PeriodID is unique
'i.e. a teacher can't be assigned 2 classes the same period
'during master schedule generation
'==========================================================
Dim strPeriodID As String
Dim strTeacherID As String
Dim strComplete As String
strPeriodID = rst![PeriodID]
strTeacherID = rst![TeacherID]
strComplete = strPeriodID & "," & strTeacherID
rst.MoveFirst
If rst![PeriodID] & "," & rst![TeacherID] = strComplete Then
Do
rst.Edit
rst![PeriodID] = Int((8 - 1 + 1) * Rnd() + 1)
Loop Until rst![PeriodID] & "," & rst![TeacherID] = Not strComplete
Else
rst.MoveNext
End If
'==========================================================
'End Experimental Code
'==========================================================
rst.MoveNext
Loop Until rst.EOF
rst.Close
Set rst = Nothing
I have a pretty tough VBA question, and I am hoping someone could point me in the right direction to fix my code. Right now I am attempting to create a generic scheduling program, and the setup I am using is a school scheduling program. The goal is to take teachers and assign the classes they are going to teach to periods of the school day. And the code works great except for one thing: the code runs through and puts a teacher teaching multiple classes during the same period.
What I have in my code is a period (1 through 8) is assigned randomly to a class/teacher, then the next record is handled and assigned a random period. What I want the code to do is compare a string value, composed of the teacher ID and period ID for any duplicates - if there are duplicates, this means that the teacher is being assigned multiple classes the same period. And when this occurs, I want the code to re-run and assign a new random period to that record until there are no duplicates.
Below is the code that I have. If I remove the portion that say "Experiemental Code", it will assign all classes, but there are duplicate periods assigned. I can also get it to loop, but I get stuck in an infinite loop on the third record. Any help fixing or providing me with suggestions for the "Experiemental Code" section would be great!
Set rst = db.OpenRecordset("tblTempMasterSchedule", dbOpenTable)
rst.MoveFirst
rst.Edit
rst![PeriodID] = Int((8 - 1 + 1) * Rnd() + 1)
rst.Update
rst.MoveNext
Do
rst.Edit
rst![PeriodID] = Int((8 - 1 + 1) * Rnd() + 1)
rst.Update
'==========================================================
'Experimental Code
'Needs to check to make sure TeacherID/PeriodID is unique
'i.e. a teacher can't be assigned 2 classes the same period
'during master schedule generation
'==========================================================
Dim strPeriodID As String
Dim strTeacherID As String
Dim strComplete As String
strPeriodID = rst![PeriodID]
strTeacherID = rst![TeacherID]
strComplete = strPeriodID & "," & strTeacherID
rst.MoveFirst
If rst![PeriodID] & "," & rst![TeacherID] = strComplete Then
Do
rst.Edit
rst![PeriodID] = Int((8 - 1 + 1) * Rnd() + 1)
Loop Until rst![PeriodID] & "," & rst![TeacherID] = Not strComplete
Else
rst.MoveNext
End If
'==========================================================
'End Experimental Code
'==========================================================
rst.MoveNext
Loop Until rst.EOF
rst.Close
Set rst = Nothing
Last edited: