S
SammyB
Part of the database I am putting together requires that I keep attendance
for a class. I found an example for what I am trying to do on another Access
help site located here:
http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=54924.
Basically, a listbox is created of all the potential attendees of a class. I
can select multiple students from the list and then choose a class ID. Once I
have the students and class ID selected, I click a button and individual
records are created in an attendance table with student IDs and the class ID.
While the example does an excellent job of keeping attendance for who
attended a particular class, I am equally interested in who did NOT attend
the class. What I would like my code to do is create an attendance record for
everyone in the list box (not just the ones selected) and use the
ctlRef.ItemData conditionally to add an ‘X’ to the attendance field of all
records that attended the class and leave a Ҡin the field for those who did
not attend. Is this possible?
Thank you in advance to anyone who can help me.
Below is the code I have used so far (take note of the commented lines):
Private Sub CREATE_ATTENDANCE_Click()
On Error GoTo Err_cmdCreate_Click
Me.TEXT_NOTICE = CreateAttendanceRecords(Me.STUDENT_LIST)
Me.STUDENT_LIST.Requery
Exit_cmdCreate_Click:
Exit Sub
Err_cmdCreate_Click:
MsgBox Err.Number & "-" & Err.Description
Resume Exit_cmdCreate_Click
End Sub
Public Function CreateAttendanceRecords(ctlRef As ListBox) As String
On Error GoTo Err_CreateAttendanceRecords_Click
Dim i As Variant
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qd As DAO.QueryDef
Set dbs = CurrentDb
Set qd = dbs.QueryDefs!qry_YR1_Attendance
Set rst = qd.OpenRecordset
For Each i In ctlRef.Recordset 'IS THIS LINE CORRECT?
rst.AddNew
rst!STUDENT_ID = ctlRef.ItemData(i)
rst!CLASS_ID = Me.CLASS
' BELOW IS THE CONDITIONAL PSEUDO CODE OF THE LOGIC I WOULD LIKE
If ctlRef.ItemsSelected = True Then
rst!Attendance = "X"
Else
rst!Attendance = ""
End If
rst.Update
Next i
' COMMENTED LINES BELOW ARE OLD CODE THAT WORKED FOR ADDING SELECTED ITEMS
ONLY
' For Each i In ctlRef.ItemsSelected
' rst.AddNew
' rst!STUDENT_ID = ctlRef.ItemData(i)
' rst!CLASS_ID = Me.CLASS
' rst!Attendance = "X"
' rst.Update
' Next i
Set rst = Nothing
Set qd = Nothing
CreateAttendanceRecords = "Records Created"
Exit_CreateAttendanceRecords_Click:
Exit Function
Err_CreateAttendanceRecords_Click:
Select Case Err.Number
Case 3022 'ignore duplicate keys
Resume Next
Case Else
MsgBox Err.Number & "-" & Err.Description
Resume Exit_CreateAttendanceRecords_Click
End Select
End Function
for a class. I found an example for what I am trying to do on another Access
help site located here:
http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=54924.
Basically, a listbox is created of all the potential attendees of a class. I
can select multiple students from the list and then choose a class ID. Once I
have the students and class ID selected, I click a button and individual
records are created in an attendance table with student IDs and the class ID.
While the example does an excellent job of keeping attendance for who
attended a particular class, I am equally interested in who did NOT attend
the class. What I would like my code to do is create an attendance record for
everyone in the list box (not just the ones selected) and use the
ctlRef.ItemData conditionally to add an ‘X’ to the attendance field of all
records that attended the class and leave a Ҡin the field for those who did
not attend. Is this possible?
Thank you in advance to anyone who can help me.
Below is the code I have used so far (take note of the commented lines):
Private Sub CREATE_ATTENDANCE_Click()
On Error GoTo Err_cmdCreate_Click
Me.TEXT_NOTICE = CreateAttendanceRecords(Me.STUDENT_LIST)
Me.STUDENT_LIST.Requery
Exit_cmdCreate_Click:
Exit Sub
Err_cmdCreate_Click:
MsgBox Err.Number & "-" & Err.Description
Resume Exit_cmdCreate_Click
End Sub
Public Function CreateAttendanceRecords(ctlRef As ListBox) As String
On Error GoTo Err_CreateAttendanceRecords_Click
Dim i As Variant
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qd As DAO.QueryDef
Set dbs = CurrentDb
Set qd = dbs.QueryDefs!qry_YR1_Attendance
Set rst = qd.OpenRecordset
For Each i In ctlRef.Recordset 'IS THIS LINE CORRECT?
rst.AddNew
rst!STUDENT_ID = ctlRef.ItemData(i)
rst!CLASS_ID = Me.CLASS
' BELOW IS THE CONDITIONAL PSEUDO CODE OF THE LOGIC I WOULD LIKE
If ctlRef.ItemsSelected = True Then
rst!Attendance = "X"
Else
rst!Attendance = ""
End If
rst.Update
Next i
' COMMENTED LINES BELOW ARE OLD CODE THAT WORKED FOR ADDING SELECTED ITEMS
ONLY
' For Each i In ctlRef.ItemsSelected
' rst.AddNew
' rst!STUDENT_ID = ctlRef.ItemData(i)
' rst!CLASS_ID = Me.CLASS
' rst!Attendance = "X"
' rst.Update
' Next i
Set rst = Nothing
Set qd = Nothing
CreateAttendanceRecords = "Records Created"
Exit_CreateAttendanceRecords_Click:
Exit Function
Err_CreateAttendanceRecords_Click:
Select Case Err.Number
Case 3022 'ignore duplicate keys
Resume Next
Case Else
MsgBox Err.Number & "-" & Err.Description
Resume Exit_CreateAttendanceRecords_Click
End Select
End Function