Cross-Tab Query Automation to Excel

G

Guest

I can get the recordset generated by an Access Cross-Tab Query to transfer into
Excel thru automation...but...

Can I also get a "Header" in the first row showing the "Field Names"
generated by the Cross-Tab Query?

TIA - Bob
 
D

David Lloyd

Bob:

Have you tried the OutputTo method? For example:

DoCmd.OutputTo acOutputQuery, "Projects_Crosstab", acFormatXLS,
"C:\testbase.xls", True

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I can get the recordset generated by an Access Cross-Tab Query to transfer
into
Excel thru automation...but...

Can I also get a "Header" in the first row showing the "Field Names"
generated by the Cross-Tab Query?

TIA - Bob
 
G

Guest

David - Thank you.

I have 15 different Queries going to the same Worksheet in a Workbook.
I clear each Named Range (thru automation) before transferring the data into
Excel.

Does this command allow "starting" at a named Range location? I thought it
just transfers that data into Cell A1 of the Worksheet??

TIA - Bob
 
D

David Lloyd

Bob:

If you need to insert to a named range the OutputTo method won't work.

I don't know all of your requirements, however, another possibility is to
"pull" the data into Excel rather than "pushing" it. You can create
database queries in Excel based on Access tables or queries and insert the
data at a specified location. You can have multiple database queries on the
same worksheet. You access this feature from the Data menu, Import External
Data, New Database Query. This does bring the field names into the
worksheet along with the data.


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


David - Thank you.

I have 15 different Queries going to the same Worksheet in a Workbook.
I clear each Named Range (thru automation) before transferring the data into
Excel.

Does this command allow "starting" at a named Range location? I thought it
just transfers that data into Cell A1 of the Worksheet??

TIA - Bob
 
G

Guest

David - I'm not an Excel Wiz. I had cross-posted this to Queries...I just
added the automation code there...

I can just type the fields above the Named ranges...more on that in the
Queries Newsgroup...I have comfortable doing that.

Public Sub CopyData(strSql As String, strWorkBook As String, _
Optional strWorkSheet As String, Optional strCellRef As String, _
Optional SSS As String, Optional TTT As String, _
Optional DEF As String, Optional GHI As String)
Dim Z As Database
On Error GoTo ProcError
DoCmd.Hourglass True
Dim objXLApp As Object 'Excel.Application
Dim objXLWb As Object 'Excel.Workbook
Dim objXLSheet As Object 'Excel.Worksheet
Dim RS As DAO.Recordset, RT As DAO.Recordset
Dim AQQ As DAO.QueryDef, PM As DAO.Parameter
Dim fld As DAO.Field, I%, iSheets%
'set rs from sql, table or query
Set Z = CurrentDb
Set AQQ = Z.QueryDefs(strSql)
For Each PM In AQQ.Parameters
PM.Value = Eval(PM.Name)
Next PM
Set RS = AQQ.OpenRecordset(, dbOpenSnapshot)
'start Excel
Set objXLApp = CreateObject("Excel.Application")
'open workbook, error routine will create it if doesn't exist
'only create workbooks with 1 sheet
iSheets = objXLApp.SheetsInNewWorkbook 'save user's setting
objXLApp.SheetsInNewWorkbook = 1 'set for only 1 sheet
Set objXLWb = objXLApp.Workbooks.Open(strWorkBook)
objXLApp.SheetsInNewWorkbook = iSheets 'restore user's setting
'select a worksheet, if sheet doesn't exist
'the error routine will add it
If strWorkSheet = "" Then strWorkSheet = "Sheet1"
'If Range is missing default to A1
If strCellRef = "" Then strCellRef = "A1"
'select desired worksheet
Set objXLSheet = objXLWb.Worksheets(strWorkSheet)
objXLSheet.Range(SSS).Clear
objXLSheet.Range(SSS) = TTT
objXLSheet.Range(strCellRef).Clear 'Is "TheData"
objXLSheet.Range(strCellRef).CopyFromRecordset RS, 50000, 150
Outa:
objXLWb.Save: objXLWb.Close
'DoCmd.SetWarnings True
'close up other rs objects
If Not RS Is Nothing Then RS.Close: Set RS = Nothing
If Not AQQ Is Nothing Then AQQ.Close: Set AQQ = Nothing
If Not Z Is Nothing Then Z.Close: Set Z = Nothing
Set objXLSheet = Nothing: Set objXLWb = Nothing
'quit Excel
If Not objXLApp Is Nothing Then objXLApp.Quit: Set objXLApp = Nothing
'DoCmd.Hourglass False
Exit Sub
ProcError:
Select Case Err
Case -2147417851 '"The server threw an exception"
' Caused when objXLSheet.Range(strCellRef).CopyFromRecordset RS
'Instead of objXLSheet.Range(strCellRef).CopyFromRecordset RS, 100, 2
Case 9 'Worksheet doesn't exist
objXLWb.Worksheets.Add
Set objXLSheet = objXLWb.ActiveSheet
objXLSheet.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
DoCmd.Hourglass False
MsgBox Err.Number & " " & Err.Description
Resume '0
End Select
End Sub
 
A

aaron.kempf

excel is crap

leave your data in a database and fire all your spreadsheet dorks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top