J
Jason Kearns
Hello,
I've run into a rather strange problem. I have a crosstab query that
shows me exactly what I want (all the assignments for all students in
all classes with their given mark). My problem is that when I make a
dynamic Crosstab Report out of this crosstab query (by following the
example in the Solutions.mdb db provided by MSDN), every record in the
report displays the same data - which is always the last record in the
crosstab query.
For example, if in my crosstab query I have:
Class Student Quiz Homework Exam
English John Doe 5 7 55
English Jane Doe 8 11 68
English Mary Joe 9 8 59
Then in my crosstab report, it would show:
Class Student Quiz Homework Exam
English John Doe 9 8 59
English Jane Doe 9 8 59
English Mary Joe 9 8 59
My crosstab query is correct. My code for generating the report looks
like:
<code>
Option Compare Database ' Use database order for string
comparisons.
Option Explicit
' Constant for maximum number of columns FinalCheckMarks query would
create
Const conTotalColumns = 12
' 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 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 Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
' Place values in text boxes and hide unused text boxes.
Dim intX As Integer
' Verify that not at end of recordset.
If Not rstReport.EOF Then
' If FormatCount is 1, place values from recordset into text
boxes
' in detail section.
If Me.FormatCount = 1 Then
For intX = 7 To intColumnCount 'I start at 7 because
there are 6 row headers before the data starts
Me("Col" + Format$(intX)) = rstReport(intX - 1)
Next intX
' Hide unused text boxes in detail section.
For intX = intColumnCount + 1 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 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 = 7 To intColumnCount 'I start at 7 because there are 6
row headers before the data starts
Me("Head" + Format$(intX)) = rstReport(intX - 1).Name
Next intX
' Hide unused text boxes in page header.
For intX = (intColumnCount + 1) To conTotalColumns
Me("Head" + Format$(intX)).Visible = False
Next intX
End Sub
Private Sub Report_Open(Cancel As Integer)
Dim intX As Integer
Dim qdf As DAO.QueryDef
' Set database variable to current database.
Set dbsReport = CurrentDb
' Open QueryDef object.
Set qdf = dbsReport.QueryDefs("qryFinalCheckMarks_Crosstab2") 'this
is the crosstab query
' 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 Report_Close()
On Error Resume Next
' Close recordset.
rstReport.Close
Set rstReport = Nothing
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 ReportHeader_Format(Cancel As Integer, FormatCount As
Integer)
' Move to first record in recordset at beginning of report
' or when 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.)
If Not rstReport.EOF And Not rstReport.BOF Then rstReport.MoveFirst
'Initialize variables.
InitVars
End Sub
</code>
I've went through this code line by line as it has run and watched
every piece of data (i.e. every mark) come into the report via the
Detail_Format subroutine. For some reason though, when it finishes
looping through every student in every class and then goes to print
preview, it displays the marks in the last record for every student in
every class. Anyone out there have any ideas?
Thanks!
JK
I've run into a rather strange problem. I have a crosstab query that
shows me exactly what I want (all the assignments for all students in
all classes with their given mark). My problem is that when I make a
dynamic Crosstab Report out of this crosstab query (by following the
example in the Solutions.mdb db provided by MSDN), every record in the
report displays the same data - which is always the last record in the
crosstab query.
For example, if in my crosstab query I have:
Class Student Quiz Homework Exam
English John Doe 5 7 55
English Jane Doe 8 11 68
English Mary Joe 9 8 59
Then in my crosstab report, it would show:
Class Student Quiz Homework Exam
English John Doe 9 8 59
English Jane Doe 9 8 59
English Mary Joe 9 8 59
My crosstab query is correct. My code for generating the report looks
like:
<code>
Option Compare Database ' Use database order for string
comparisons.
Option Explicit
' Constant for maximum number of columns FinalCheckMarks query would
create
Const conTotalColumns = 12
' 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 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 Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
' Place values in text boxes and hide unused text boxes.
Dim intX As Integer
' Verify that not at end of recordset.
If Not rstReport.EOF Then
' If FormatCount is 1, place values from recordset into text
boxes
' in detail section.
If Me.FormatCount = 1 Then
For intX = 7 To intColumnCount 'I start at 7 because
there are 6 row headers before the data starts
Me("Col" + Format$(intX)) = rstReport(intX - 1)
Next intX
' Hide unused text boxes in detail section.
For intX = intColumnCount + 1 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 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 = 7 To intColumnCount 'I start at 7 because there are 6
row headers before the data starts
Me("Head" + Format$(intX)) = rstReport(intX - 1).Name
Next intX
' Hide unused text boxes in page header.
For intX = (intColumnCount + 1) To conTotalColumns
Me("Head" + Format$(intX)).Visible = False
Next intX
End Sub
Private Sub Report_Open(Cancel As Integer)
Dim intX As Integer
Dim qdf As DAO.QueryDef
' Set database variable to current database.
Set dbsReport = CurrentDb
' Open QueryDef object.
Set qdf = dbsReport.QueryDefs("qryFinalCheckMarks_Crosstab2") 'this
is the crosstab query
' 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 Report_Close()
On Error Resume Next
' Close recordset.
rstReport.Close
Set rstReport = Nothing
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 ReportHeader_Format(Cancel As Integer, FormatCount As
Integer)
' Move to first record in recordset at beginning of report
' or when 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.)
If Not rstReport.EOF And Not rstReport.BOF Then rstReport.MoveFirst
'Initialize variables.
InitVars
End Sub
</code>
I've went through this code line by line as it has run and watched
every piece of data (i.e. every mark) come into the report via the
Detail_Format subroutine. For some reason though, when it finishes
looping through every student in every class and then goes to print
preview, it displays the marks in the last record for every student in
every class. Anyone out there have any ideas?
Thanks!
JK