Question : Dynamic Crosstab Report doesn't stop at end of recordset

So I was having exactly the same problem as the OP of the related question and I thought this solution had totally fixed it. In fact, having added my underlying table as the recordsource for the report, I could indeed see all of the rows from the crosstab query, but for some reason, the last row of my query is repeated over and over (for 155 pages of the report!!) and all of these rows are added to the total (making my total wildly wrong). I'm using the same code from the same MS article as the OP, but I AM using the forms. I've attached the code anyway.
Can anyone shed any light on this for me?
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:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
Option Compare Database
Option Explicit
 
   '  Constant for maximum number of columns
   Const conTotalColumns = 9
 
   '  Variables for Database object and Recordset.
   Dim dbsReport As DAO.Database
   Dim rstReport As DAO.Recordset
 
   '  Variables for number of columns and row and report totals.
   Dim intColumnCount As Integer
   Dim lngRgColumnTotal(1 To conTotalColumns) As Long
   Dim lngReportTotal As Long
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
   ' Put values in text boxes and hide unused text boxes.
    
   Dim intX As Integer
   '  Verify that you are not at end of recordset.
   If Not rstReport.EOF Then
      '  If FormatCount is 1, put values from recordset into text boxes
      '  in "Detail" section.
      If Me.FormatCount = 1 Then
         For intX = 1 To intColumnCount
            '  Convert Null values to 0.
            Me("Col" + Format(intX)) = xtabCnulls(rstReport(intX - 1))
         Next intX
    
         '  Hide unused text boxes in the "Detail" section.
         For intX = intColumnCount + 2 To conTotalColumns
            Me("Col" + Format(intX)).Visible = False
         Next intX
 
         '  Move to next record in recordset.
         rstReport.MoveNext
      End If
 
   End If
        
End Sub
Private Sub InitVars()
      
   Dim intX As Integer
 
   ' Initialize lngReportTotal variable.
   lngReportTotal = 0
    
   ' Initialize array that stores column totals.
   For intX = 1 To conTotalColumns
      lngRgColumnTotal(intX) = 0
   Next intX
 
End Sub
 
 
Private Function xtabCnulls(varX As Variant)
     
   ' Test if a value is null.
   If IsNull(varX) Then
      ' If varX is null, set varX to 0.
      xtabCnulls = 0
   Else
      ' Otherwise, return varX.
      xtabCnulls = varX
   End If
 
End Function
 
 
 
Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
 
   Dim intX As Integer
   Dim lngRowTotal As Long
 
   '  If PrintCount is 1, initialize rowTotal variable.
   '  Add to column totals.
   If Me.PrintCount = 1 Then
      lngRowTotal = 0
 
      For intX = 2 To intColumnCount
         '  Starting at column 2 (first text box with crosstab value),
         '  compute total for current row in the "Detail" section.
         lngRowTotal = lngRowTotal + Me("Col" + Format(intX))
 
         '  Add crosstab value to total for current column.
         lngRgColumnTotal(intX) = lngRgColumnTotal(intX) + Me("Col" + Format(intX))
      Next intX
 
      '  Put row total in text box in the "Detail" section.
      Me("Col" + Format(intColumnCount + 1)) = lngRowTotal
      '  Add row total for current row to grand total.
      lngReportTotal = lngReportTotal + lngRowTotal
   End If
End Sub
 
 
Private Sub Detail_Retreat()
 
   ' Always back up to previous record when "Detail" section retreats.
   rstReport.MovePrevious
 
End Sub
 
 
Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
    
   Dim intX As Integer
    
   '  Put column headings into text boxes in page header.
   For intX = 1 To intColumnCount
      Me("Head" + Format(intX)) = rstReport(intX - 1).Name
   Next intX
 
   '  Make next available text box Totals heading.
   Me("Head" + Format(intColumnCount + 1)) = "Totals"
 
   '  Hide unused text boxes in page header.
   For intX = (intColumnCount + 2) To conTotalColumns
      Me("Head" + Format(intX)).Visible = False
   Next intX
 
End Sub
 
 
Private Sub Report_Close()
 
   On Error Resume Next
 
   '  Close recordset.
   rstReport.Close
 
End Sub
 
 
Private Sub Report_NoData(Cancel As Integer)
 
   MsgBox "No records match the criteria you entered.", vbExclamation, "No Records Found"
   rstReport.Close
   Cancel = True
 
End Sub
 
Private Sub Report_Open(Cancel As Integer)
 
   '  Create underlying recordset for report
    
   Dim intX As Integer
   Dim qdf As QueryDef
   Dim frm As Form
 
   '  Set database variable to current database.
   Set dbsReport = CurrentDb
   Set frm = Forms!SelectDate
   '  Open QueryDef object.
   Set qdf = dbsReport.QueryDefs("Query1")
   ' Set parameters for query based on values entered
 
   qdf.Parameters("Forms!SelectDate!StartDate") = frm!StartDate
   qdf.Parameters("Forms!SelectDate!EndDate") = frm!EndDate
 
   '  Open Recordset object.
   Set rstReport = qdf.OpenRecordset()
   
   '  Set a variable to hold number of columns in crosstab query.
   intColumnCount = rstReport.Fields.Count
    
End Sub
Private Sub ReportFooter_Print(Cancel As Integer, PrintCount As Integer)
    
   Dim intX As Integer
 
   '  Put column totals in text boxes in report footer.
   '  Start at column 2 (first text box with crosstab value).
   For intX = 2 To intColumnCount
      Me("Tot" + Format(intX)) = lngRgColumnTotal(intX)
   Next intX
 
   '  Put grand total in text box in report footer.
   Me("Tot" + Format(intColumnCount + 1)) = lngReportTotal
 
   '  Hide unused text boxes in report footer.
   For intX = intColumnCount + 2 To conTotalColumns
      Me("Tot" + Format(intX)).Visible = False
   Next intX
 
End Sub
Private Sub ReportHeader_Format(Cancel As Integer, FormatCount As Integer)
 
   '  Move to first record in recordset at the beginning of the report
   '  or when the report is restarted. (A report is restarted when
   '  you print a report from Print Preview window, or when you return
   '  to a previous page while previewing.)
   rstReport.MoveFirst
 
   'Initialize variables.
   InitVars
 
End Sub 
Open in New Window Select All
Open in New Window Select All

Answer : Dynamic Crosstab Report doesn't stop at end of recordset

ollyb303,
In the report design change Record Source to Query1. Currently it is set to table!
Random Solutions  
 
programming4us programming4us