Question : How do I programmatically set the recordsource in a subreport?

I have a report with a sub report.  Both the parent and child reports display data based on a user-provided date range.  The sub report, when not embedded in the parent, works properly.  When it is being opened in the parent report, however, I get the following error message:

"Run-time error '2191':  You can't set the record source property in print preview or after printing has started."

Attached is the code that runs when the sub report opens.

Would you kindly let me know how I can make this work.
Thank you.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
Private Sub Report_Open(Cancel As Integer)
    Me.RecordSource = "SELECT tbl_CourseGivenExtraClass.*, " & _
        " [FirstName] & ' ' & [LastName]" & _
        " AS StudentName" & _
        " FROM (tbl_CourseGivenExtraClass" & _
        " INNER JOIN tbl_lnk_Student_ExtraClass" & _
        " ON tbl_CourseGivenExtraClass.IdCourseGivenExtraClass =" & _
        " tbl_lnk_Student_ExtraClass.IdCourseGivenExtraClass)" & _
        " INNER JOIN tbl_Students" & _
        " ON tbl_lnk_Student_ExtraClass.IdStudent =" & _
        " tbl_Students.IdStudent" & _
        " WHERE (((tbl_CourseGivenExtraClass.CourseDate)" & _
        " >= #" & GetLogStartDate & "# And" & _
        " (tbl_CourseGivenExtraClass.CourseDate)" & _
        " <= #" & GetLogEndDate & "#))" & _
        " ORDER BY tbl_CourseGivenExtraClass.CourseDate," & _
        " tbl_CourseGivenExtraClass.CourseTime," & _
        " [FirstName] & ' ' & [LastName];"
End Sub

Answer : How do I programmatically set the recordsource in a subreport?

Set the recordsource of the subreport in design mode
Random Solutions  
 
programming4us programming4us