VB Code:
Public Function exportData(pPath As String, pDataSource As Integer)
On Error GoTo Err_exportData
Dim oApp As Excel.Application
Dim oWB As Excel.Workbook
Dim oWS As Excel.Worksheet
Dim i, j, tDataSource As Integer
Dim tPath, tTemp, tSQL As String
Dim tResult As VbMsgBoxResult
If Not pDataSource = -1 Then
tDataSource = pDataSource
Else
tDataSource = 5
End If
tSQL = Switch(tDataSource = 0, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=7)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
tDataSource = 1, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=8)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
tDataSource = 2, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=9)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
tDataSource = 3, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=10)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
tDataSource = 4, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=11)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
tDataSource = 5, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;")
tPath = pPath
tTemp = Right(tPath, 1)
If Not tTemp = "\" Then
If Not tTemp = "/" Then
tPath = tPath & "\"
End If
End If
tTemp = Date
tPath = tPath & Replace(tTemp, "/", "_")
If Len(Dir(tPath & ".xls")) > 0 Then
' We know that the file exists.
tResult = MsgBox("File Already Exists, would you like to overwrite the existing file?", vbYesNo, "PK:Sanctions")
If tResult = vbNo Then
MsgBox "No file created", vbOKOnly
Exit Function
Else
Dim tDeleteFile As String
tDeleteFile = tPath & ".xls"
SetAttr tDeleteFile, vbNormal
Kill tDeleteFile
End If
End If
Set oApp = New Excel.Application
Set oWB = oApp.Workbooks.Add
DoCmd.RunSQL ("CREATE TABLE TempTable (Surname Text(50), Forename Text(50), [Date] DateTime, Sanction Text(50), Reason Text(255), ExclusionLength Long, TeacherName Text(50), FacultyName Text(50), Period Text(50));")
DoCmd.RunSQL (tSQL)
oWB.Sheets("Sheet1").Cells(1, 1) = ""
oWB.Close SaveChanges:=True, Filename:=tPath
Set oWB = Nothing
oApp.Quit
Set oApp = Nothing
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "TempTable", tPath & ".xls"
DoCmd.RunSQL ("DROP TABLE TempTable;")