Question : Insert Page break in Detail after each 5 records

I have attached a database with a report named rptAttendForm that will be used to print an attendance sheet for a training class that can span any number of days.  The class info (name, classID, Company who we're teaching, etc.) are in the table tblClasses which is what the report is bound to.  There is another table named tblClassSchedule that holds the specific dates and times that the sessions will be held.

On the attendance sheet I need to print the class dates into the Date text boxes that are named txtDate1, txtDate2, txtDate3, txtDate4, and txtDate5 (if you look in DesignView I colored them Red).  

Since the number of dates in a given schedule vary, I will need to be able to insert a page break after the first 5, next 5, etc.    So, for example, if the class is schedule from 1-5  days we would have 1 attendance page, if there are between 6-10 days 2 pages, etc.  In the sample database there is one class that meets on 9 different dates, so there would be two pages of the attendance sheet, the first with the first 5 dates, the 2nd with the next 4, then the report footer would need to print on a 3rd page.

I've attached code that I put in the form's Detail's Paint event (might need some direction on this if that would not be the appropriate place) .

Here's where I need help - figuring out how to ge the loop to work and how to make it do a page break and repeat the detail section with the next series of dates.  Also I'm wondering if there's a way to set an object variable that could be used to increment the text box names (txtDate1, txtDate2, etc.) and then reference them when filling the text box with the classDate.  

Hope I'm making sense:)  Thanks for all your help in advance.



Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
Dim D As Database
    Dim R As Recordset
    Dim X As Long
    Dim Y As Long
    Dim txtBox As Access.Control
    
    Dim numRecords As Long
    Dim numPages As Double
    Dim totPages As Long
    Dim intNumPages As Long
    
    Set D = CurrentDb
    Set R = D.OpenRecordset("Select * from tblClassSchedule WHERE ClassID = " & Me.ClassID & " ORDER by ClassDate", dbOpenDynaset)
    R.MoveLast
    numRecords = R.RecordCount
    numPages = numRecords / 5
    
    intNumPages = Int(numPages)
    
    If numPages > intNumPages Then
      totPages = intNumPages + 1
    Else
      totPages = intNumPages
    End If
    'MsgBox "TotPages is " & totPages
    
    R.MoveFirst
    For X = 1 To numRecords 'loop through all the records in tblClassSchedule for a specific ClassID
        
        For Y = 1 To 5 'place the class dates in txtDate1 through txtDate5
            
        Next Y
        R.MoveNext
    Next X

Answer : Insert Page break in Detail after each 5 records

This would be easier if only one 'detail' row existed per page and it contained five columns for the five dates. That way the textboxes on the report could be bound and there's no need to control the paging.
Why not write code to repopulate a temporary table each time the report is run:
           tblDates(ClassID, D1,D2,D3,D4,D5)
The code loops through tblClassSchedule to add one row for each collection of five dates for a class.

Link this table to the other tables in the record source for your form and you should be there.
Random Solutions  
 
programming4us programming4us