Link to home
Start Free TrialLog in
Avatar of newbie46
newbie46

asked on

Resetting recordsource for each record in subreport

I am using the following code (within a sub report) to set the record source. The problem is that I need to reset it for each new student id (within the main report) and the same course names are appearing for each student.

Private Sub Report_Open(Cancel As Integer)

Dim strSQL As String

On Error GoTo Err_Report_Open

 strSQL = "SELECT tbl_Courses.CourseName " & _
          "FROM tbl_Courses " & _
          "WHERE (tbl_Courses.Season='Summer') AND " & _
          "(tbl_Courses.Student=" & Me.Parent!StudentId & ")"
   
   Me.RecordSource = strSQL
.
.

Previously, I used the following code:
Private Sub Detail_Format (.....
dim cnn as new ADODB.Connection
dim rs as new adodb.recordset
dim strsql as string
set cnn = codeproject.connection

strsql = "SELECT tbl_Courses.CourseName " & _
          "FROM tbl_Courses " & _
          "WHERE (tbl_Courses.Season='Summer') AND " & _
          "(tbl_Courses.Student=" & Me.Parent!StudentId & ")"
rs.open source:=strsql, activeconnection:=cnn

do while not rs.eof
  me.CourseTaken = rs!CourseName
  rs.movenext
loop

The problem with this method was that only the last coursetaken was displayed.

Any suggestions?
thanks.

All students are retrieved from the record source of the main report. The subreport needs to loop through multiple records associated with a particular student to display their courses taken.
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

The simplest way is to allow Access to handle this for you. After properly building your report/subreport (and binding them to the correct Recordsource), make sure to set the Master/Child link fields for the subreport object. This tells Access which records to display for the "main" report record ... in effect, Access already does your filtering for you.
Avatar of newbie46
newbie46

ASKER

So are you suggesting to use the  Detail_Format Sub code? or suggesting something else?
Since we use ADPs, having our stored procedures stored in SQL Server 2008, we don't typically use the Master/Child link fields. They don't seem to work correctly with ADPs (so I have been told)
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial