Marie,
this is the most simple code to export to excel using copy from recordset.
The notes are just below, before the start of the code
To use the code instead of TransferSpreadsheet
Replace DoCmd.TransferSpreadsheet with
Call CopyRecordset2XL
'------------------------------------------------------------------------
'replace "c:\documents and settings\Jeanette\desktop\SimpleCopyRs.xls"
'with your own file name and path
'replace QueryName with the query you are exporting
'strSQL can be a saved query,
'or a saved table,
'or a sql statement
'strSql = "SELECT yadda, yadda " _
' & "FROM yadda " _
' & "WHERE yadda " _
' & "ORDER BY yadda"
'------------------------------------------------------------------------
Public Sub CopyRecordset2XL()
On Error GoTo SubErr
Dim objXLApp As Object 'Excel.Application
Dim objXLWb As Object 'Excel.Workbook
Dim objXLWs As Object 'Excel.Worksheet
Dim strWorkBook As String 'name of workbook
Dim strWorkSheet As String 'name of worksheet
Dim lngSheets As Long 'sheet number
Dim rst As DAO.Recordset
Dim lngCount As Long 'counter
Dim strSQL As String 'data to export
'turn on the hourglass
DoCmd.Hourglass True
'name and full path to use to save the xls file
strWorkBook = "c:\documents and settings\" _
& "Jeanette\desktop\SimpleCopyRs.xls"
'name of the recordset to copy
strSQL = "QueryName"
'set rst from sql, table or query
Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
'If there are no records, skip the rest of the code
If rst.EOF Then
'handle error here
Else
'start Excel
Set objXLApp = CreateObject("Excel.Application")
'open workbook, error routine will
'create it if doesn't exist
'only create workbooks with 1 sheet
lngCount = objXLApp.SheetsInNewWorkbook 'save user's setting
objXLApp.SheetsInNewWorkbook = 1 'set for only 1 sheet
Set objXLWb = objXLApp.Workbooks.Open(strWorkBook)
objXLApp.SheetsInNewWorkbook = lngCount 'restore user's setting
'select the worksheet, if sheet doesn't exist
'the error routine will add it
strWorkSheet = "Sheet1"
'select desired worksheet
Set objXLWs = objXLWb.Worksheets(strWorkSheet)
'copy the recordset into the worksheet
'starting at cell A2
objXLWs.Range("A2").CopyFromRecordset rst
objXLWs.Columns.AutoFit
'Save wb
objXLWb.Save
objXLWb.Close
End If
'**error handling, in the sub exit
'make sure you set the object references
'to nothing as shown below.
SubExit:
Set objXLWs = Nothing
Set objXLApp = Nothing
' Destroy the recordset object
rst.Close
If Not rst Is Nothing Then
Set rst = Nothing
End If
'turn off the hourglass
DoCmd.Hourglass False
Exit Sub
SubErr:
Select Case Err
Case 9 'Worksheet doesn't exist
objXLWb.Worksheets.Add
Set objXLWs = objXLWb.ActiveSheet
objXLWs.Name = strWorkSheet
Resume Next
Case 1004 'Workbook doesn't exist, make it
objXLApp.Workbooks.Add
Set objXLWb = objXLApp.ActiveWorkbook
objXLWb.SaveAs strWorkBook
Resume Next
Case Else
MsgBox Err.Description & " " & Err.Number
Resume SubExit
End Select
End Sub