Hi Gary
Not sure what you mean by "used as part of the code module..."
Sorry - I meant form code
you have this saved query above that works,
but does not work when you...
1) write out the SQL in code
2) reference the saved query in code
Correct
might help if you provide code?
Will do so at end but its very long!
It looks like other query that works does not
apply a WHERE filter on the PIVOT field.
The working queries reference a different field in the table with on
possible repeated headings!
Was one variation?
(if eliminate Null/ZLS in totals part of query,
then no need to reuse PIVOT field
in TRANSFORM)
Sorry - don't understand this comment
TRANSFORM Count(*) AS CountOfAtt
SELECT Subjects.SubjectID
FROM
(Subjects
INNER JOIN
(PupilGrades
INNER JOIN
ClassModules
ON
PupilGrades.ModuleID = ClassModules.ModuleID)
ON
Subjects.SubjectID = ClassModules.SubjectID)
INNER JOIN
Classes
ON
ClassModules.ClassID = Classes.ClassID
WHERE
(
(Len(Trim((PupilGrades.Att) & '')) > 0)
AND
((Classes.Year)=GetYearGroup())
AND
((PupilGrades.SessionID)=GetSession())
)
GROUP BY
Subjects.SubjectID
ORDER BY
Subjects.SubjectID
PIVOT
PupilGrades.Att;
Tried it with same result as my original SQL
Where does 'N/A' come from?
Its one of the possible grade values for PupilGrades.Att field
Are some Att values 'N/A'? 'Yes
Is it a PIVOT column in the saved query
that works? Yes
Here is the complete code for the ExportToExcel sub:
Apologies for the length...!
-------------------------------------------------------
Sub ExportToExcel()
On Error GoTo ExportToExcel_err
Dim strFilename As String
Dim strAlias As String
Dim rst, rst2 As Recordset
Dim ExApp As Object
Dim MyRange As Object
Dim stFilename1, stFilename2 As String
Dim strsql, strSQLx, strSQLLabels As String
Dim intRows, iCount, icount2, iFields As Integer
Dim ColCount1, ColCount2, ColCount3, ModuleCount As Integer
Dim varResults
Dim FldName(50)
ExcelHeader = strHeader & strSession
Select Case strCallForm
Case "IntAtt"
strSQL1 = "SELECT Grades.Code" & _
" From Grades" & _
" WHERE (((Grades.Type) = 'A') And ((Grades.KeyStage) = GetKeyStage()))" & _
" ORDER BY Grades.Points DESC, Grades.Order;"
'New SQL works fine as query but causes error 3104 here
strRecordSource = "TRANSFORM Count(PupilGrades.Att) AS CountOfAtt" & _
" SELECT Subjects.SubjectID" & _
" FROM (Subjects INNER JOIN (PupilGrades INNER JOIN ClassModules" & _
" ON PupilGrades.ModuleID = ClassModules.ModuleID) ON
Subjects.SubjectID = ClassModules.SubjectID)" & _
" INNER JOIN Classes ON ClassModules.ClassID = Classes.ClassID" & _
" WHERE (((PupilGrades.Att) <> '') AND
((Classes.Year)=GetYearGroup()) AND ((PupilGrades.SessionID)=GetSession()))"
& _
" GROUP BY Subjects.SubjectID" & _
" ORDER BY Subjects.SubjectID" & _
" PIVOT PupilGrades.Att"
'Alternative SQL by Gary Walter from Access Newsgroup
'also works fine as query but causes error 3104 here
'strRecordSource = "TRANSFORM Count(*) AS CountOfAtt" & _
" SELECT Subjects.SubjectID" & _
" FROM Classes INNER JOIN (Subjects INNER JOIN (PupilGrades" & _
" INNER JOIN ClassModules ON PupilGrades.ModuleID = ClassModules.ModuleID)"
& _
" ON Subjects.SubjectID = ClassModules.SubjectID) ON Classes.ClassID =
ClassModules.ClassID" & _
" WHERE (((Len(Trim(([PupilGrades].[Att]) & '')))>0) AND
((Classes.Year)=GetYearGroup())" & _
" AND ((PupilGrades.SessionID)=GetSession()))" & _
" GROUP BY Subjects.SubjectID" & _
" ORDER BY Subjects.SubjectID" & _
" PIVOT PupilGrades.Att"
'Original SQL causes duplicate grades run as query e.g. N/A & also fails
here with error 3104
'strRecordSource = "TRANSFORM Count(PupilGrades.Att) AS CountOfAtt" & _
" SELECT Subjects.SubjectID" & _
" FROM (Grades INNER JOIN (Subjects INNER JOIN (PupilGrades INNER JOIN
ClassModules" & _
" ON PupilGrades.ModuleID = ClassModules.ModuleID) ON Subjects.SubjectID =
ClassModules.SubjectID)" & _
" ON Grades.Code = PupilGrades.Att) INNER JOIN Classes ON
ClassModules.ClassID = Classes.ClassID" & _
" WHERE (((Classes.Year)=GetYearGroup()) AND
((PupilGrades.SessionID)=GetSession())" & _
" AND ((Grades.KeyStage)=GetKeyStage()) AND ((Grades.Type)='A'))" & _
" GROUP BY Subjects.SubjectID, Grades.KeyStage, Grades.Type" & _
" ORDER BY Subjects.SubjectID" & _
" PIVOT PupilGrades.Att"
Case "IntEff"
strSQL1 = "SELECT Grades.Code" & _
" From Grades" & _
" WHERE (((Grades.Type) = 'E') And ((Grades.KeyStage) = GetKeyStage()))" & _
" ORDER BY Grades.Points DESC, Grades.Order;"
'this works fine
strRecordSource = "TRANSFORM Count(PupilGrades.Eff) AS CountOfEff" & _
" SELECT Classes.SubjectID" & _
" FROM (Classes INNER JOIN ClassModules ON Classes.ClassID =
ClassModules.ClassID)" & _
" INNER JOIN (Grades INNER JOIN PupilGrades ON Grades.Code =
PupilGrades.Eff)" & _
" ON ClassModules.ModuleID = PupilGrades.ModuleID" & _
" WHERE (((Classes.Year)=GetYearGroup()) AND
((PupilGrades.SessionID)=GetSession()) AND ((Grades.KeyStage)=GetKeyStage())
AND ((Grades.Type)='E'))" & _
" GROUP BY Classes.SubjectID" & _
" ORDER BY Classes.SubjectID" & _
" PIVOT PupilGrades.Eff"
Case "IntPG"
strSQL1 = "SELECT Grades.Code" & _
" From Grades" & _
" WHERE (((Grades.Type) = 'PG') And ((Grades.KeyStage) = GetKeyStage()))" & _
" ORDER BY Grades.Order;"
'This works fine
strRecordSource = "TRANSFORM Count(PupilGrades.PGGrade) AS CountOfPGGrade" & _
" SELECT Classes.SubjectID" & _
" FROM (Classes INNER JOIN ClassModules ON Classes.ClassID =
ClassModules.ClassID)" & _
" INNER JOIN (Grades INNER JOIN PupilGrades ON Grades.Code =
PupilGrades.PGGrade)" & _
" ON ClassModules.ModuleID = PupilGrades.ModuleID" & _
" WHERE (((Classes.Year)=GetYearGroup()) AND
((PupilGrades.SessionID)=GetSession()) AND ((Grades.KeyStage)=GetKeyStage())
AND ((Grades.Type)='PG'))" & _
" GROUP BY Classes.SubjectID" & _
" ORDER BY Classes.SubjectID" & _
" PIVOT PupilGrades.PGGrade"
Case "IntPGStr"
strSQL1 = "SELECT Grades.Code" & _
" From Grades" & _
" WHERE (((Grades.Type) = 'P2') And ((Grades.KeyStage) = GetKeyStage()))" & _
" ORDER BY Grades.Order"
'This works fine
strRecordSource = "TRANSFORM Count(PupilGrades.PGStr1) AS CountOfPGStr1" & _
" SELECT Classes.SubjectID" & _
" FROM (Classes INNER JOIN ClassModules ON Classes.ClassID =
ClassModules.ClassID)" & _
" INNER JOIN (Grades INNER JOIN PupilGrades ON Grades.Code =
PupilGrades.PGStr1)" & _
" ON ClassModules.ModuleID = PupilGrades.ModuleID" & _
" WHERE (((Classes.Year)=GetYearGroup()) AND
((PupilGrades.SessionID)=GetSession()) AND ((Grades.KeyStage)=GetKeyStage())
AND ((Grades.Type)='P2'))" & _
" GROUP BY Classes.SubjectID" & _
" ORDER BY Classes.SubjectID" & _
" PIVOT PupilGrades.PGStr1"
Case "EOYAtt"
strSQL1 = "SELECT Grades.Code" & _
" From Grades" & _
" WHERE (((Grades.Type) = 'A') And ((Grades.KeyStage) = GetKeyStage()))" & _
" ORDER BY Grades.Points DESC, Grades.Order"
'SQL causes duplicate grades run as query e.g. N/A & also fails here with
error 3104
strRecordSource = "TRANSFORM Count(ClassRecords.Att) AS CountOfAtt" & _
" SELECT Classes.SubjectID" & _
" FROM (Classes INNER JOIN ClassRecords ON Classes.ClassID =
ClassRecords.ClassID)" & _
" INNER JOIN Grades ON ClassRecords.Att = Grades.Code" & _
" WHERE (((Classes.Year) = GetYearGroup()) And ((Classes.ShowOnReport) =
Yes)" & _
" AND ((Grades.KeyStage) = GetKeyStage())" & _
" AND ((Grades.Type) = 'A'))" & _
" GROUP BY Classes.SubjectID" & _
" PIVOT ClassRecords.Att"
Case "EOYEff"
strSQL1 = "SELECT Grades.Code" & _
" From Grades" & _
" WHERE (((Grades.Type) = 'E') And ((Grades.KeyStage) = GetKeyStage()))" & _
" ORDER BY Grades.Order"
'This works fine
strRecordSource = "TRANSFORM Count(ClassRecords.Att) AS CountOfEff" & _
" SELECT Classes.SubjectID" & _
" FROM (Classes INNER JOIN ClassRecords ON Classes.ClassID =
ClassRecords.ClassID)" & _
" INNER JOIN Grades ON ClassRecords.Eff = Grades.Code" & _
" WHERE (((Classes.Year) = GetYearGroup()) And ((Classes.ShowOnReport) =
Yes)" & _
" AND ((Grades.KeyStage) = GetKeyStage())" & _
" AND ((Grades.Type) = 'E'))" & _
" GROUP BY Classes.SubjectID" & _
" PIVOT ClassRecords.Eff"
Case "EOYCr"
strSQL1 = "SELECT Grades.Code" & _
" From Grades" & _
" WHERE (((Grades.Type) = 'CR') And ((Grades.KeyStage) = GetKeyStage()))" & _
" ORDER BY Grades.Points DESC, Grades.Order"
'SQL causes duplicate grades run as query e.g. N/A & also fails here with
error 3104
strRecordSource = "TRANSFORM Count(ClassRecords.Att) AS CountOfCRGrade" & _
" SELECT Classes.SubjectID" & _
" FROM (Classes INNER JOIN ClassRecords ON Classes.ClassID =
ClassRecords.ClassID)" & _
" INNER JOIN Grades ON ClassRecords.CRGrade = Grades.Code" & _
" WHERE (((Classes.Year) = GetYearGroup()) And ((Classes.ShowOnReport) =
Yes)" & _
" AND ((Grades.KeyStage) = GetKeyStage())" & _
" AND ((Grades.Type) = 'CR'))" & _
" GROUP BY Classes.SubjectID" & _
" PIVOT ClassRecords.CRGrade"
End Select
Debug.Print strRecordSource
'get the column headers for the grades and determine the order - by points
for Att,CRGrade
' and by 'Order' for other grades
Dim VarIn As String
VarIn = ""
Set rst2 = mydb.OpenRecordset(strSQL1)
If Not (rst2.BOF Or rst2.EOF) Then
rst2.MoveFirst
Do Until rst2.EOF
VarIn = VarIn & "'" & rst2!Code & "', "
rst2.MoveNext
Loop
rst2.Close
VarIn = Left(VarIn, Len(VarIn) - 2)
End If
strRecordSource = strRecordSource & " In(" & VarIn & ")"
'get the data from the selected fields and field names
Set rst = CurrentDb.OpenRecordset(strRecordSource)
If rst.BOF Or rst.EOF Then
MsgBox "There are no records"
rst.Close
Exit Sub
End If
'Get the filename for the Excel Spreadsheet
stFilename1 = MSA_SimpleGetSaveFileName
If stFilename1 = "" Then Exit Sub
If right(stFilename1, 4) <> ".xls" Then
stFilename1 = stFilename1 & ".xls"
End If
strText1 = stFilename1
stFilename2 = StripPath(stFilename1)
strFilename = CurrentDBDir & "GradeDistributions.xls"
'defineExcel objects
Set ExApp = CreateObject("Excel.Application")
ExApp.Visible = True
ExApp.WorkBooks.Open strFilename
ExApp.WorkBooks("GradeDistributions.xls").SaveAs (stFilename1)
iCount = 0
With rst
.MoveLast
.MoveFirst
intRows = .RecordCount
ColCount1 = .Fields.Count
For iCount = 0 To ColCount1 - 1
FldName(iCount) = rst.Fields(iCount).Name
Next iCount
varResults = rst.GetRows(intRows + 1)
'remove any null values from the array
For icount2 = 0 To intRows - 1
For iCount = 0 To ColCount1 - 1
varResults(iCount, icount2) = Nz(varResults(iCount, icount2), " ")
Next iCount
Next icount2
.Close
End With
'remove unwanted columns
' ExApp.Columns(5).Select
' ExApp.Selection.Delete Shift:=-4159
' ExApp.Columns(2).Select
' ExApp.Selection.Delete Shift:=-4159
'populate sreadsheet with data
Set MyRange = ExApp.activesheet.cells(3, 1).Resize(intRows, ColCount1)
MyRange.FormulaArray = ExApp.Transpose(varResults)
'add field names
Set MyRange = ExApp.activesheet.cells(2, 1).Resize(1, ColCount1)
MyRange.FormulaArray = FldName
ExApp.Range("A1").SELECT
'add header
Set MyRange = ExApp.activesheet.cells(1, 1)
MyRange.FormulaArray = "Grade distributions by Subject: " & ExcelHeader
ExportToExcel_Exit:
On Error Resume Next
ExApp.ActiveWorkbook.Close True
ExApp.WorkBooks.Close
ExApp.Quit
Set ExApp = Nothing
Dim stmessage As String
stmessage = "Do you want to use the Excel file now?"
If MsgBox(stmessage, vbYesNo) = vbYes Then
DoCmd.Close acForm, "GradesByFaculty"
Dim RetVal
strText1 = "EXCEL.EXE """ & stFilename1 & """"
RetVal = Shell(strText1, vbNormalFocus)
End If
Exit Sub
ExportToExcel_err:
MsgBox "Error # " & err.Number & " " & vbNewLine & err.Description & "
", vbExclamation, "Crosstab query error"
'Resume ExportToExcel_Exit
End Sub