Based on that query, you could probably use a sub like the following.
Watch out for line wrap in the newsreader inserting extra lines.
One thing to watch out for is students with the same First and Last name
in the same homeroom. A rare occurence, but it could happen. You would
be safer with a unique identifier (student id number) or just an
autonumber to identify each student record. That way there would be no
confusion within the database.
You can put this sub into the form's code module or you could insert the
code minus the first and last lines into the click event of a button on
your form F_ReportsCards.
If you do put it into the form's module, you can call it from the click
event of the button
WARNING: The following code will compile but it has not been executed to
run.
Also make sure you change the line
DoCmd.OpenReport "Name_Of_Your_Report", acViewNormal, , strWhere
and insert the actual name of your report between the quotes.
Good luck.
Sub sPrintReportCards()
Dim strWhere As String
Dim strSQL As String
Dim db As DAO.Database
Dim rst As DAO.Recordset
'============================================================================
' Step 1 construct a recordset of all students meeting the criteria
'============================================================================
strSQL = "SELECT Distinct T_Student.HR, T_Student.Lname" & _
", T_Student.FName" & _
" FROM T_Student" & _
" WHERE T_Student.HR=""" & [Forms]![F_ReportsCards]![cmbHR] & """" & _
" AND T_Student.Lname Like """ & _
[Forms]![F_ReportsCards]![cmbStudent] & "*""" & _
" ORDER BY T_Student.HR, T_Student.Lname, T_Student.FName"
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL)
'============================================================================
' Step 2 loop through all the records that were returned in the recordset
'============================================================================
If rst.RecordCount < 1 Then
MsgBox "Sorry no match for that name and Homeroom combination"
Else
While Not rst.EOF
strWhere = "HR = """ & rst!HR & _
""" AND Lname = """ & rst!Lname & """" & _
""" AND FName = """ & rst!Fname & """"
DoCmd.OpenReport "NameOfYourReport", acViewNormal, , strWhere
DoEvents 'Allow break to process data
rst.MoveNext
Wend
End If
End Sub
John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
Thanks John. Below is the Sql you requested. Any help would be greatly
appreciate.
SELECT T_Student.HR, T_Student.Lname, T_Student.Fname, T_Student!Fname
& " " & T_Student!Lname AS Name, T_Student.Grade
FROM T_Student
GROUP BY T_Student.HR, T_Student.Lname, T_Student.Fname,
T_Student!Fname & " " & T_Student!Lname, T_Student.Grade
HAVING (((T_Student.HR)=[Forms]![F_ReportsCards]![cmbHR]) AND
((T_Student.Lname) Like [Forms]![F_ReportsCards]![cmbStudent] & "*"))
ORDER BY T_Student.HR, T_Student.Lname;