Here is an example:
'Get Plan Data
strSQL = "SELECT Sum(Plan_res_export.SumOfJAN) AS JAN, " _
& "Sum(Plan_res_export.SumOfFEB) AS FEB, " _
& "Sum(Plan_res_export.SumOfMAR) AS MAR,
Sum(Plan_res_export.SumOfAPR) AS APR, " _
& "Sum(Plan_res_export.SumOfMAY) AS MAY,
Sum(Plan_res_export.SumOfJUN) AS JUN, " _
& "Sum(Plan_res_export.SumOfJul) AS Jul,
Sum(Plan_res_export.SumOfAUG) AS AUG, " _
& "Sum(Plan_res_export.SumOfSEP) AS SEP,
Sum(Plan_res_export.SumOfOCT) AS OCT, " _
& "Sum(Plan_res_export.SumOfNOV) AS NOV,
Sum(Plan_res_export.SumOfDec) AS Dec " _
& "FROM Plan_res_export " _
& "GROUP BY Plan_res_export.BillCat, Plan_res_export.ITM,
Plan_res_export.[SubTask] " _
& "HAVING (((Plan_res_export.BillCat)= '" & strResource & "') AND "
_
& "((Plan_res_export.ITM)= '" & strItm & "') AND " _
& "((Plan_res_export.[SubTask])= '" & strRecurring & "'));"
If strItmPM = "PM" Then
strSQL = Replace(strSQL, "Plan_res_export.ITM",
"Plan_res_export.[Program Manager]")
End If
Set rstPlan = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot,
dbReadOnly)
If rstPlan.EOF Then
xlSheet.Range("B29", "M29").Value = 0
Else
xlSheet.Cells(29, 2).CopyFromRecordset rstPlan
End If
--
Dave Hargis, Microsoft Access MVP
Dale Fye said:
Dave,
I've never used the CopyFromRecordset method. I'll have to take a look.
Dale
--
email address is invalid
Please reply to newsgroup only.
:
Good approach, Dale. The only thing different I would suggest is
rather than
exporting the data to Excel would be to create the workbook using
Automation
then use the CopyFromRecordset method. The advantage is you can place
the
data where ever you want it rather than having in start in A1.
Another technique I use which improves performance and keeps the user
from
messing up the spreadsheet while it is being built is to set these
properties:
xlApp.DisplayAlerts = False
xlApp.Interactive = False
xlApp.ScreenUpdating = False
That will cause the Excel sheet to be hidden and saves the time of
Excel
repainting the screen repeatedly. Then, when everything is done, set
them
back to true and the spreadsheet appears.
Also, as the OP wanted, it has yet to be saved to a file.
--
Dave Hargis, Microsoft Access MVP
:
In addition to posting to the Excel groups, you might want to try the
Office
Automation newsgroups. There are lots of examples there of
automating Excel
from Access.
VBA gives you the ability to export the results of a query to an
Excel
spreadsheet, but you can also then open the spreadsheet through
automation,
and VBA to set properties, and formatting of the Excel spreadsheet.
Generally, the way I start this is to output the file to Excel, then
I open
the Excel file, and record a macro so I can see how Excel does it,
when I
format a block of data. Then, I go back to access and copy/modify
the EXCEL
macro into Access VBA. Although not require, it helps if you
initially set a
reference to the Excel Object model, since this provides
intellisense. Once
I have the automation working the way I want, I generally remove the
reference to Excel and replace the dimension statemets to specific
Excel
object types (Workbook, Worksheet, Cell) with assignments as Object.
This
late binding ensures that if a user doesn't have the same version of
Access
that you are using, or has a different version of Excel, that the
code will
be more likely to run on their version.
--
Don''t forget to rate the post if it was helpful!
email address is invalid
Please reply to newsgroup only.
:
this is very helpful already. thank you very much. two follow up
questions:
1) Would you also know how I can add for example an auto filter,
freeze the
panes, autoformat the columns?
2) I tried also to auto-format the columns in a query output.
However this
seems not to work. My columns change as they are based on different
selections?
:
If you use the "Analyze it with Microsoft Excel" function, it
will
automatically create a spreadsheet file in your default database
folder and
open it for the user, so the user will have to delete that file
afterwards.
Here is an example of how to call a stored query (QueryName) and
automatically export it to Excel without TransferSpreadsheet:
DoCmd.OpenQuery "QueryName", acViewNormal
RunCommand acCmdOutputToExcel
Once the user is finished analyzing the spreadsheet, the user can
press a
button that contains the code to delete the file:
Kill Application.GetOption("Default Database Directory") &
"\QueryName.
xls"
Chris
Microsoft MVP
hannes wrote:
I would like to display my result on a dynamic SQL query not in
Access rather
directly in Microsoft Excel. However I do not like to safe the
file or ask
the user where to store it. When I run the query and display it
the simple
solution now is to click the "Export to Microsoft Excel" office
link. This
would do what I need, however I would like to do this
automatically. Does
anyone has an idea how this works?