I think it's better to use DAO.
Here's how
(copy and paste the following code into a new module):
Option Compare Database
Option Explicit
' This module requires the following references:
'
' 1. Microsoft DAO Object Library
' 2. Microsoft Excel Object Library
'
' (To create the references, in the VBA editor,
' open the Tools Menu, select References, and
' then select the two above object libraries.)
Sub DXReport()
' DX REPORT:
'
' This subprocedure declares constants
' specific to the DX Report.
' Other reports (if any) can use
' different values in these constants
' and still call the next subprocedure
' CopyFromRecordsetToXL().
' Declare constants:
Const strcRecordSource As String = "ABI_XL"
Const strcWorkBookPath As String = _
"r:\temp\ResultsReportingWorksheet.xls"
Const strcStartCopyingAt As String = "B20"
Const strcWorkbookSavePath As String = _
"r:\temp\"
Const strcWorkbookSaveName As String = _
"ResultsReportingWorksheet.xls"
Dim strNow As String
Dim strSaveName As String
' Create a new SaveAs name with a
' date/time prefix.
' Note: The date prefix is in reverse
' (YYYYMMDD) order so files will be listed
' in date order in Windows Explorer.
strNow = Format(Now(), "YYYYMMDD HHNN")
strSaveName = strcWorkbookSavePath _
& strNow & " " & strcWorkbookSaveName
' Call generic routine:
Call CopyFromRecordsetToXL(strcRecordSource, _
strcWorkBookPath, strcStartCopyingAt, _
strSaveName)
End Sub
Sub CopyFromRecordsetToXL(strRecordSource As String, _
strWorkBookPath As String, _
strStartCopyingAt As String, _
strSaveAsName As String)
' Access objects:
Dim objDB As DAO.Database
Dim objRST As DAO.Recordset
' Excel objects:
Dim objXL As Excel.Application
Dim objWBK As Excel.Workbook
Dim objWS As Excel.Worksheet
Dim objRNG As Excel.Range
On Error GoTo Error_CopyFromRecordsetToXL
' Start Excel:
On Error Resume Next
Set objXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
On Error GoTo Error_CopyFromRecordsetToXL
Set objXL = CreateObject("Excel.Application")
End If
On Error GoTo Error_CopyFromRecordsetToXL
With objXL
.Visible = True
.WindowState = xlMinimized
End With
Set objWBK = objXL.Workbooks.Open(strWorkBookPath)
Set objWS = objWBK.Worksheets(1)
Set objRNG = objWS.Range(strStartCopyingAt)
' DAO objects:
Set objDB = CurrentDb()
Set objRST = objDB.OpenRecordset(strRecordSource)
' Copy data:
objRNG.CopyFromRecordset objRST
' Save Excel Workbook using a different name:
objWBK.SaveAs strSaveAsName
Exit_CopyFromRecordsetToXL:
' Destroy DAO objects:
If Not objRST Is Nothing Then
objRST.Close
Set objRST = Nothing
End If
Set objDB = Nothing
' Destroy Excel objects:
Set objRNG = Nothing
Set objWS = Nothing
If Not objWBK Is Nothing Then
' Don't save changes here!
objWBK.Close SaveChanges:=False
Set objWBK = Nothing
End If
If Not objXL Is Nothing Then
objXL.Quit
Set objXL = Nothing
End If
Exit Sub
Error_CopyFromRecordsetToXL:
MsgBox "Error Number: " & Err.Number _
& vbNewLine _
& Err.Description, _
vbExclamation + vbOKOnly, _
"Error Information"
Resume Exit_CopyFromRecordsetToXL
End Sub
Geoff
mls via AccessMonster.com said:
I am trying to write to Excel and have this code.. which fails at line
Dim
Rst1 As New ADODB.Recordset, saying "User definded type not defined" .
Also
after witrring I want to rename my template
Any thoughts?
Sub xl()
Dim DXrptPath As String
DXrptPath = "r:\temp\Resultsreportingworksheet.xls"
'***Opening Report Template
Dim myDB As Database
Set myDB = CurrentDb
Set ExcelWindow = CreateObject("excel.application")
ExcelWindow.Visible = True
ExcelWindow.Workbooks.Open (DXrptPath)
Dim DXrpt As Workbook
Set DXrpt = ActiveWorkbook
DXrpt.Application.WindowState = xlMinimized
Dim Rst1 As New ADODB.Recordset
'Set Rst1 = myDB.OpenRecordset("ABI_XL")
With Rst1
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.ActiveConnection = CurrentProject.Connection
.Open "[ABI_XL]"
End With
DXrpt.Worksheets(1).Range("b20:k20").CopyFromRecordset Rst1
End Sub