Cross-Tab Query & Access-to-Excel Automation...

G

Guest

Cross-Post from General Questions = NO Replies there yet...

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

Duane Hookom

"automation" creates several possibilities/solution. You might want to share
at least the structure of your automation code.
 
G

Guest

Duane - thank you.

I can just type in the Header Fields above the transferred data. I transfer
a Start date (thru automation), so I can write formulas to show the Cross-tab
date values.

Here's the Subroutine w/ comments, etc.

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

TIA - Bob
 
D

Duane Hookom

You can loop through the fields of the recordset and place the names of the
fields above the strCellRef.
 
G

Guest

Duane - Interesting..Yes, that would work.

Is that likely the only way? No "wholesale" copy-in of the Fields names as
the
1st row???

So I have an Apple & an Orange...loop thru the code of 15 Queries of around
30 Fields each (it's a Forecast for 18-months)....OR...Hard code the Fields &
adjust for the Month Titles.

Thank you..Bob
 
D

Duane Hookom

From you code, you look like you know what you are doing with code. Consider
writing a single function that accepts a DAO recordset and a cell address.
The function would loop through the field names in the recordset and
beginning from the cell, enter the first field, move right, enter the next
field....
 
G

Guest

Duane - thank you.

I agree w/ your recommendation.

I went ahead and wrote formulas for the 18-month Forecast Months that run
from an automated "Start Date" transfer. I went ahead & entered the other
Field Names in a row above the Named Range. It was quick.

Thank you again - Bob
 

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