Gasman
Enthusiastic Amateur
- Local time
- Today, 19:43
- Joined
- Sep 21, 2011
- Messages
- 17,499
Yes,
I have just taken that code and amended it.
I created a query to get some data and called the function.
The data appears in the sheet as shown in the attached pic.
My Access did hang twice, but I *think* that was because I did not realise that the sheet was open elsewhere.
Not much has changed, just the file names and I commented out the second .CopyFromRecordset (xlWSh.Range("D8").CopyFromRecordset rst) as the recordset would not have changed and we'd just be pasting to the next row.
You are not doing anything with that date in the code for the button, so can only presume it is being used in your query?
Do you know how to debug code, setting breakpoints and stepping through code line by line?
My only problem is getting a decent name in the Save As line, but the data is good.
I have just taken that code and amended it.
I created a query to get some data and called the function.
The data appears in the sheet as shown in the attached pic.
My Access did hang twice, but I *think* that was because I did not realise that the sheet was open elsewhere.
Not much has changed, just the file names and I commented out the second .CopyFromRecordset (xlWSh.Range("D8").CopyFromRecordset rst) as the recordset would not have changed and we'd just be pasting to the next row.
You are not doing anything with that date in the code for the button, so can only presume it is being used in your query?
Do you know how to debug code, setting breakpoints and stepping through code line by line?
My only problem is getting a decent name in the Save As line, but the data is good.
Code:
Option Compare Database
Option Explicit
Function SendToExcel(strTQName As String, strSheetName As String)
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As DAO.Field
Dim lngMaxRow As Long
Dim lngMaxCol As Long
Dim lngLastRow As Long
Dim N As String
Dim strPath As String
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
Const xlUp As Long = -4162
Const xlDown As Long = -4121
On Error GoTo Err_Handler
strPath = "C:\Temp\AccessExport.xls"
Set rst = CurrentDb.OpenRecordset(strTQName)
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(strPath)
'Moved to end so Excel doesn't open before the report is finished rendering
'ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets(strSheetName)
rst.MoveFirst
xlWSh.Range("C8").CopyFromRecordset rst
' xlWSh.Range("D8").CopyFromRecordset rst
' selects the first cell to unselect all cells
xlWSh.Range("A1").Select
rst.Close
Set rst = Nothing
'Remove prompt to save file
ApXL.DisplayAlerts = False
xlWBk.SaveAs "C:\Temp\" & strTQName & ".xls", 51
ApXL.DisplayAlerts = True
'Open after report is completes
ApXL.Visible = True
'ApXL.Quit
Exit Function
Err_Handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
ApXL.Quit
Set ApXL = Nothing
Exit Function
End Function