John W. Vinson said:
You're apparently assuming that you must have a *table* in order to export
to
Excel. That assumption is incorrect; it's perfectly possible and normal to
export a *query* to Excel. See the VBA help for TransferSpreadsheet.
Hi, John:
It's not simple export to Excel. I need to put course information on top of
Excel rows and copy student info below. Here are some of my code:
strYear = rs!YYYY ' Year
strSem = rs!SEMESTER ' Semester
strSubj = rs!Subj ' Course Subject
strNo = rs!No ' Course No
strSect = rs!Sect ' Section
strEmail = rs!Email ' email address for the instructor
' Create an file name by combining all the above except email so it
is unique for each subject.
strSubject = strYear & " " & strSem & " " & strSubj & " " & strNo &
" - " & strSect
strFileName = strSubject & ".xlsx"
With objXL
.Visible = False 'Hide Excel
Set objWkb = .Workbooks.Open(conWKB_NAME)
On Error Resume Next
Set objSht = objWkb.Worksheets("Sheet1") 'The step and the
next is important when working with more than one worksheet
Err.Clear
On Error GoTo 0
With objSht
Set rs1 = db.OpenRecordset("tblTmpStudent", dbOpenSnapshot)
'Copy data from the two record sets
On Error Resume Next
.Range("C8").CopyFromRecordset rs1 'Copy students
.Range("D1").Value = rs!YYYY & " " & rs!SEMESTER 'Copy
header info
.Range("D2").Value = rs!Sect & " " & rs!Subj & " " & rs!No
.Range("D3").Value = rs!Instructor
.Range("D4").Value = rs!Begin
End With
End With
objXL.DisplayAlerts = False
objWkb.SaveAs strPath & "\" & strFileName
objSht.Close
objWkb.Close
Set objSht = Nothing
Set objWkb = Nothing
objXL.DisplayAlerts = True
Call SendEMail(strEmail, "", strSubject, "", "S:\Dirk\" & strFileName,
True)