Theo ý kiến của một bạn muốn xếp phòng thi theo môn đăng ký của học sinh. mình gửi cho các bạn đoạn code sau tham khảo.
code:
Private Sub Command0_Click()
Dim SlHs(10 To 12, 1 To 8) As Integer ' so luong hs khoi i (10 to 12) dang ky mon j (1 to 8)
Dim SlPhong(10 To 12, 1 To 8) As Integer ' so luong phong cua khoi i dang ky mon j
Dim SlHsPhongCuoi(10 To 12, 1 To 8) As Integer ' so luong hs cua phong cuoi cua khoi i dang ky mon j
Dim dem(10 To 12, 1 To 8) As Integer ' dem so luong hs sap vao phong cua khoi i dang ky mon j
Dim phong(10 To 12, 1 To 8) As Integer ' dang sap phong cua khoi i dang ky mon j
' ------------------- dem sl hoc sinh
For i = 10 To 12
For j = 1 To 8
tieuchuan = "[khoi] = '" & i & "' and [dk" & j & "] = TRUE"
SlHs(i, j) = DCount("*", "hocsinh", tieuchuan)
Next
Next
'---------------------tinh sl phõng
For i = 10 To 12
For j = 1 To 8
SlPhong(i, j) = SlHs(i, j) \ 24 + IIf(SlHs(i, j) Mod 24 > 4, 1, 0)
Next
Next
'------------------------- tinh sl hs phong cuoi
For i = 10 To 12
For j = 1 To 8
SlHsPhongCuoi(i, j) = IIf(SlHs(i, j) Mod 24 > 4, SlHs(i, j) Mod 24, SlHs(i, j) Mod 24 + 24)
Next
Next
'------------------------- gan bien dem = 1
For i = 10 To 12
For j = 1 To 8
dem(i, j) = 1
Next
Next
'------------------------- gan bien phong = 1
For i = 10 To 12
For j = 1 To 8
phong(i, j) = 1
Next
Next '----------------
Dim bang As Recordset
Set bang = CurrentDb.OpenRecordset("sapphong", dbOpenDynaset)
If Not bang.BOF Then
bang.MoveFirst
End If
'---------------
Do While Not bang.EOF
'---------------mon 1
If bang![dk1] = True Then
mon = 1
bang.Edit
bang!phongM1 = Format(phong(Val(bang!khoi), mon), "00")
bang.Update
If phong(Val(bang!khoi), mon) < SlPhong(Val(bang!khoi), mon) And dem(Val(bang!khoi), mon) = 24 Then
phong(Val(bang!khoi), mon) = phong(Val(bang!khoi), mon) + 1
dem(Val(bang!khoi), mon) = 1
Else
dem(Val(bang!khoi), mon) = dem(Val(bang!khoi), mon) + 1
End If
End If
'---------------mon 2
If bang![dk2] = True Then
mon = 2
bang.Edit
bang!phongM2 = Format(phong(Val(bang!khoi), mon), "00")
bang.Update
If phong(Val(bang!khoi), mon) < SlPhong(Val(bang!khoi), mon) And dem(Val(bang!khoi), mon) = 24 Then
phong(Val(bang!khoi), mon) = phong(Val(bang!khoi), mon) + 1
dem(Val(bang!khoi), mon) = 1
Else
dem(Val(bang!khoi), mon) = dem(Val(bang!khoi), mon) + 1
End If
End If
'---------------mon 3
If bang![dk3] = True Then
mon = 3
bang.Edit
bang!phongM3 = Format(phong(Val(bang!khoi), mon), "00")
bang.Update
If phong(Val(bang!khoi), mon) < SlPhong(Val(bang!khoi), mon) And dem(Val(bang!khoi), mon) = 24 Then
phong(Val(bang!khoi), mon) = phong(Val(bang!khoi), mon) + 1
dem(Val(bang!khoi), mon) = 1
Else
dem(Val(bang!khoi), mon) = dem(Val(bang!khoi), mon) + 1
End If
End If
'---------------mon 4
If bang![dk4] = True Then
mon = 4
bang.Edit
bang!phongM4 = Format(phong(Val(bang!khoi), mon), "00")
bang.Update
If phong(Val(bang!khoi), mon) < SlPhong(Val(bang!khoi), mon) And dem(Val(bang!khoi), mon) = 24 Then
phong(Val(bang!khoi), mon) = phong(Val(bang!khoi), mon) + 1
dem(Val(bang!khoi), mon) = 1
Else
dem(Val(bang!khoi), mon) = dem(Val(bang!khoi), mon) + 1
End If
End If
'---------------mon 5
If bang![dk5] = True Then
mon = 5
bang.Edit
bang!phongM5 = Format(phong(Val(bang!khoi), mon), "00")
bang.Update
If phong(Val(bang!khoi), mon) < SlPhong(Val(bang!khoi), mon) And dem(Val(bang!khoi), mon) = 24 Then
phong(Val(bang!khoi), mon) = phong(Val(bang!khoi), mon) + 1
dem(Val(bang!khoi), mon) = 1
Else
dem(Val(bang!khoi), mon) = dem(Val(bang!khoi), mon) + 1
End If
End If
'---------------mon 6
If bang![dk6] = True Then
mon = 6
bang.Edit
bang!phongM6 = Format(phong(Val(bang!khoi), mon), "00")
bang.Update
If phong(Val(bang!khoi), mon) < SlPhong(Val(bang!khoi), mon) And dem(Val(bang!khoi), mon) = 24 Then
phong(Val(bang!khoi), mon) = phong(Val(bang!khoi), mon) + 1
dem(Val(bang!khoi), mon) = 1
Else
dem(Val(bang!khoi), mon) = dem(Val(bang!khoi), mon) + 1
End If
End If
'---------------mon 7
If bang![dk7] = True Then
mon = 7
bang.Edit
bang!phongM7 = Format(phong(Val(bang!khoi), mon), "00")
bang.Update
If phong(Val(bang!khoi), mon) < SlPhong(Val(bang!khoi), mon) And dem(Val(bang!khoi), mon) = 24 Then
phong(Val(bang!khoi), mon) = phong(Val(bang!khoi), mon) + 1
dem(Val(bang!khoi), mon) = 1
Else
dem(Val(bang!khoi), mon) = dem(Val(bang!khoi), mon) + 1
End If
End If
'---------------mon 8
If bang![dk8] = True Then
mon = 8
bang.Edit
bang!phongM8 = Format(phong(Val(bang!khoi), mon), "00")
bang.Update
If phong(Val(bang!khoi), mon) < SlPhong(Val(bang!khoi), mon) And dem(Val(bang!khoi), mon) = 24 Then
phong(Val(bang!khoi), mon) = phong(Val(bang!khoi), mon) + 1
dem(Val(bang!khoi), mon) = 1
Else
dem(Val(bang!khoi), mon) = dem(Val(bang!khoi), mon) + 1
End If
End If
'-------------------
bang.MoveNext
Loop
bang.Close
MsgBox "Da chia phong xong", , "TB"
End Sub
Downloat file:
https://www.mediafire.com/?0oaz0hr0cdoqvgy