I have two tables. "Student" and "Class" with a PK-FK relationship.
I have a main form "Student Record" whose "Record Source" is set to "Class". I have a subform "Student subform" in the main form whose "Record Source" is set to "Student".
The subform is linked to the main form using "Class_Student ID" as the "Link Master Fields" and "Student_ID" as the "Link Child Fields".
I also have a combo box "cmbClassTitle" in the main form header whose "Row Source" is set to
Code:
SELECT Class.[Class ID], Class.[Class Title] FROM Class ORDER BY Class.[Class ID];
Now, I want to display all "Class Title" values in the combo box (which it is showing). Upon selecting a value, I want to display the enrolled student's details in the subform. This part is not working.
I have written two different sets of code for that.
Set 1
Code:
strSQL = "SELECT Student.[Student Name], Student.[Student DOB], Student.[Student Address]" & _
" FROM Student RIGHT OUTER JOIN Class" & _
" ON Student.Student_ID = Class.Class_Student_ID" & _
" WHERE Class.Class_Title = '" & Me.cmbClassTitle & "';"
[Form_Student subform].Form.RecordSource = strSQL
Running the form shows up the input box asking "Class_Title", "Student_ID" and "Class_Student_ID". 
Set 2
Code:
Set dbase = CurrentDb()
Set qdef = dbase.QueryDefs("Enrolled Student of Class")
qdef.Parameters("Class Title") = cmbClassTitle
Set rs = qdef.OpenRecordset()
If Not rs.BOF And Not rs.EOF And rs.Fields.Count > 0 Then
Set [Form_Student Record].Recordset = rs
Else
MsgBox "No corresponding record for " & cmbClassTitle & "!", vbCritical
Exit Sub
End If
Running the form in this case produces error.
Run-time error '3265':
Item not found in this collection.
The error is on line
Code:
qdef.Parameters("Class Title") = cmbClassTitle
The SQL query code for "Enrolled Student of Class" is
Code:
SELECT Student.[Student Name], Student.[Student DOB], Student.[Student Address]
FROM Student RIGHT JOIN Class ON Student.Student_ID=Class.Class_Student_ID
WHERE Class.Class_Title=[Class Title];
I'm clueless, hopelessly.