What do you mean "iterate through the .itemsSelected collection of the listbox?"
Basically you loop through the listbox and grab the selected items.
I use my own function for this as I tend to use a lot of list boxes.
Code:
Public Function getLBX(lbx As ListBox, Optional intColumn As Variant = 0, Optional Seperator As String = ",", _
Optional Delim As Variant = Null) As String
'Iterates thru the multiselect listbox and constructs an array of the selected items
'Arguments:
'Lbx is Listbox Object ie.Me.MyListbox
'intColumn is the column # to be returned
'Seperator is the character seperating items in array returned
'Delim is optional delimiter to be return in array ie. #1/1/2001#,#12/25/2015#
Dim strList As String
Dim varSelected As Variant
'On Error GoTo getLBX_Error
If lbx.ItemsSelected.Count = 0 Then
'MsgBox "Nothing selected"
Else
For Each varSelected In lbx.ItemsSelected
If Nz(lbx.Column(intColumn, (varSelected)), "") <> "" Then
strList = strList & Delim & lbx.Column(intColumn, (varSelected)) & Delim & Seperator
Else
strList = strList
End If
Next varSelected
If Nz(strList, "") <> "" Then
strList = Left$(strList, Len(strList) - 1) 'remove trailing comma
End If
End If
getLBX = strList
On Error GoTo 0
Exit Function
getLBX_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure getLBX of Module modLBX"
End Function
The getLBX code will return a comma delimited string of the selected items like - 1,2,5,9
you can use something like
Code:
dim strSql as String
dim varItm as variant
varItm = split(getLBX,Me.MyListBoxName),",")
for i = 0 to ubound(varItm)
strSql = "Insert into tblCourseEnrollment (StudentID_FK, CourseID_FK) values (" & studentID & "," & varItm(i) & ")"
CurrentDb.Execute strSql,dbfailOnError
next i
obviously insert your listbox name, fields and such into the above.