Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 19th, 2007, 08:46 AM
Authorized User
 
Join Date: Jan 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Export Recordsets to Excel

My aim is to open from a command, several SQL views exporting each individual view into it's own worksheet within one excel file.

I then hope to create a report involving charts pointing to the data from the worksheets.

My problem so far is that I can only insert the first View into the worksheet(1), the second recordset I open overwrites the first.

My code so far

Code:
Private Sub cmdViewStats_Click()

Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset

Dim xl As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Dim strSql As String


strSql = "Select * from View_AllSchoolVisits"


Set xl = CreateObject("excel.Application")

Set xlBook = Workbooks.Add("C:\test.xls")

Set cmd = New ADODB.Command

cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText
cmd.CommandText = strSql


Set rs = cmd.Execute

xl.Visible = True
xlBook.Windows(1).Visible = True

Set xlSheet = xlBook.Worksheets(1)

xl.Cells(2, 1).CopyFromRecordset rs


strSql = "Select * from view_OfficersTotalTimeSpentAtSchool"

Set rs = Nothing

cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText
cmd.CommandText = strSql


Set rs = cmd.Execute

Set xlSheet = xlBook.Worksheets(2)

xlBook.Windows(1).Visible = True

xl.Cells(2, 1).CopyFromRecordset rs 


Set rs=Nothing
Set cmd=Nothing
Set xl=Nothing
 
Old October 19th, 2007, 10:44 AM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Instead of using xl which assumes the active sheet you have to either make the target sheet active or specify the sheet specifically.
right after:
Set xlSheet = xlBook.Worksheets(2)
Do this:
xlSheet.Activate

Personally I'd just reference the spreadsheet, myself:
xlSheet.Cells(2,1).CopyFromRecordset rs

I usually iterate through the data myself so I can manipulate the data and handle it differently based on content of record / duplication elimination from joins.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Export to Excel Suresh62 Access 0 May 19th, 2008 06:38 AM
Export to Excel maigoro ASP.NET 3.5 Basics 5 April 10th, 2008 11:32 PM
Export Excel abdou Classic ASP Basics 6 February 18th, 2007 09:18 PM
Export To Excel JaCkSoN Tay VS.NET 2002/2003 2 June 30th, 2005 07:57 PM
Export to Excel rweide Classic ASP Basics 5 February 23rd, 2005 09:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.