PC Review


Reply
Thread Tools Rate Thread

Append CrossTab query result to EXCEL

 
 
=?Utf-8?B?VFM=?=
Guest
Posts: n/a
 
      8th Feb 2005
hi all,
I only have one row of record in an access query that
I want to append to an excel sheet. That means I want the code to loop
through the rows in Excel till finding an empty row and copy the access
recordset into it. I posted this request before, but haven't gotten a correct
answer yet. The code I used is the following. Please help

Private Sub cmd_ChildrenCountCharts_Click()
> Dim strxlfile As String
> Dim xlapp As Excel.Application
> Dim xlbook As Excel.Workbook
> Dim xlsheet As Excel.Worksheet
> Dim rs As Recordset
>
> strxlfile = "M:excel files\qry_ProgramTotals.xls"
> Set db = CurrentDb
> Set rs = db.OpenRecordset("qry_ProgramTotals", dbOpenSnapshot)
>
> Set xlapp = CreateObject("excel.application")
> With xlapp
> .Visible = True
> .WindowState = xlMinimized
> End With
> Set xlbook = xlapp.Workbooks.Open(strxlfile)
> Set xlsheet = xlbook.Worksheets("Programs Total")
> xlsheet.Cells.Range("a1").Select
> Do Until IsEmpty(ActiveCell)
> ActiveCell.Offset(1, 0).Select
> Loop
> ActiveCell.Offset(1, 0).CopyFromRecordset rs
>
> Set xlapp = Nothing
> Set xlbook = Nothing
> Set xlsheet = Nothing
> rs.Close
> Set rs = Nothing
>
> End Sub




--
TS
 
Reply With Quote
 
 
 
 
Stephen Haley
Guest
Posts: n/a
 
      9th Feb 2005
You dont say what is wrong but it looks fine except you have offset by one
additional row as when you exit the loop you are already on an empty cell

> Do Until IsEmpty(ActiveCell)
> ActiveCell.Offset(1, 0).Select
> Loop
> ActiveCell.Offset(1, 0).CopyFromRecordset rs

you exit the loop on an empty cell then offset again
last line should be
ActiveCell.CopyFromRecordset rs

but you are better off not moving the cursor
ie
dim x as integer
x=0
Do Until IsEmpty(ActiveCell.Offset(x,0))
x=x+1
Loop
ActiveCell.Offset(x, 0).CopyFromRecordset rs

also you need to save the file before terminating excel

xlbook.save

rgds

Stephen

"TS" <(E-Mail Removed)> wrote in message
news:11EA6F7B-89B5-4147-B3E7-(E-Mail Removed)...
> hi all,
> I only have one row of record in an access query that
> I want to append to an excel sheet. That means I want the code to loop
> through the rows in Excel till finding an empty row and copy the access
> recordset into it. I posted this request before, but haven't gotten a
> correct
> answer yet. The code I used is the following. Please help
>
> Private Sub cmd_ChildrenCountCharts_Click()
>> Dim strxlfile As String
>> Dim xlapp As Excel.Application
>> Dim xlbook As Excel.Workbook
>> Dim xlsheet As Excel.Worksheet
>> Dim rs As Recordset
>>
>> strxlfile = "M:excel files\qry_ProgramTotals.xls"
>> Set db = CurrentDb
>> Set rs = db.OpenRecordset("qry_ProgramTotals", dbOpenSnapshot)
>>
>> Set xlapp = CreateObject("excel.application")
>> With xlapp
>> .Visible = True
>> .WindowState = xlMinimized
>> End With
>> Set xlbook = xlapp.Workbooks.Open(strxlfile)
>> Set xlsheet = xlbook.Worksheets("Programs Total")
>> xlsheet.Cells.Range("a1").Select
>> Do Until IsEmpty(ActiveCell)
>> ActiveCell.Offset(1, 0).Select
>> Loop
>> ActiveCell.Offset(1, 0).CopyFromRecordset rs
>>
>> Set xlapp = Nothing
>> Set xlbook = Nothing
>> Set xlsheet = Nothing
>> rs.Close
>> Set rs = Nothing
>>
>> End Sub

>
>
>
> --
> TS



 
Reply With Quote
 
=?Utf-8?B?VFM=?=
Guest
Posts: n/a
 
      9th Feb 2005
Thanks for your response. I made the change you suggested in the code and got
the error message: Select method of range class failed when the code reached
the following: xlsheet.Cells.Range("a1").Select

Please help!! I know I am very close to solve this thanks to you.

"Stephen Haley" wrote:

> You dont say what is wrong but it looks fine except you have offset by one
> additional row as when you exit the loop you are already on an empty cell
>
> > Do Until IsEmpty(ActiveCell)
> > ActiveCell.Offset(1, 0).Select
> > Loop
> > ActiveCell.Offset(1, 0).CopyFromRecordset rs

> you exit the loop on an empty cell then offset again
> last line should be
> ActiveCell.CopyFromRecordset rs
>
> but you are better off not moving the cursor
> ie
> dim x as integer
> x=0
> Do Until IsEmpty(ActiveCell.Offset(x,0))
> x=x+1
> Loop
> ActiveCell.Offset(x, 0).CopyFromRecordset rs
>
> also you need to save the file before terminating excel
>
> xlbook.save
>
> rgds
>
> Stephen
>
> "TS" <(E-Mail Removed)> wrote in message
> news:11EA6F7B-89B5-4147-B3E7-(E-Mail Removed)...
> > hi all,
> > I only have one row of record in an access query that
> > I want to append to an excel sheet. That means I want the code to loop
> > through the rows in Excel till finding an empty row and copy the access
> > recordset into it. I posted this request before, but haven't gotten a
> > correct
> > answer yet. The code I used is the following. Please help
> >
> > Private Sub cmd_ChildrenCountCharts_Click()
> >> Dim strxlfile As String
> >> Dim xlapp As Excel.Application
> >> Dim xlbook As Excel.Workbook
> >> Dim xlsheet As Excel.Worksheet
> >> Dim rs As Recordset
> >>
> >> strxlfile = "M:excel files\qry_ProgramTotals.xls"
> >> Set db = CurrentDb
> >> Set rs = db.OpenRecordset("qry_ProgramTotals", dbOpenSnapshot)
> >>
> >> Set xlapp = CreateObject("excel.application")
> >> With xlapp
> >> .Visible = True
> >> .WindowState = xlMinimized
> >> End With
> >> Set xlbook = xlapp.Workbooks.Open(strxlfile)
> >> Set xlsheet = xlbook.Worksheets("Programs Total")
> >> xlsheet.Cells.Range("a1").Select
> >> Do Until IsEmpty(ActiveCell)
> >> ActiveCell.Offset(1, 0).Select
> >> Loop
> >> ActiveCell.Offset(1, 0).CopyFromRecordset rs
> >>
> >> Set xlapp = Nothing
> >> Set xlbook = Nothing
> >> Set xlsheet = Nothing
> >> rs.Close
> >> Set rs = Nothing
> >>
> >> End Sub

> >
> >
> >
> > --
> > TS

>
>
>

 
Reply With Quote
 
Stephen Haley
Guest
Posts: n/a
 
      10th Feb 2005
you are missing 1 line and strictly speaking you should activate the cell
(select is for a range)
xlsheet.activate

ie
xlsheet.Activate
xlsheet.Range("a1").Activate
Do Until IsEmpty(ActiveCell)


"TS" <(E-Mail Removed)> wrote in message
news:69F4FA1C-1FCF-445D-B03F-(E-Mail Removed)...
> Thanks for your response. I made the change you suggested in the code and
> got
> the error message: Select method of range class failed when the code
> reached
> the following: xlsheet.Cells.Range("a1").Select
>
> Please help!! I know I am very close to solve this thanks to you.
>
> "Stephen Haley" wrote:
>
>> You dont say what is wrong but it looks fine except you have offset by
>> one
>> additional row as when you exit the loop you are already on an empty cell
>>
>> > Do Until IsEmpty(ActiveCell)
>> > ActiveCell.Offset(1, 0).Select
>> > Loop
>> > ActiveCell.Offset(1, 0).CopyFromRecordset rs

>> you exit the loop on an empty cell then offset again
>> last line should be
>> ActiveCell.CopyFromRecordset rs
>>
>> but you are better off not moving the cursor
>> ie
>> dim x as integer
>> x=0
>> Do Until IsEmpty(ActiveCell.Offset(x,0))
>> x=x+1
>> Loop
>> ActiveCell.Offset(x, 0).CopyFromRecordset rs
>>
>> also you need to save the file before terminating excel
>>
>> xlbook.save
>>
>> rgds
>>
>> Stephen
>>
>> "TS" <(E-Mail Removed)> wrote in message
>> news:11EA6F7B-89B5-4147-B3E7-(E-Mail Removed)...
>> > hi all,
>> > I only have one row of record in an access query that
>> > I want to append to an excel sheet. That means I want the code to loop
>> > through the rows in Excel till finding an empty row and copy the access
>> > recordset into it. I posted this request before, but haven't gotten a
>> > correct
>> > answer yet. The code I used is the following. Please help
>> >
>> > Private Sub cmd_ChildrenCountCharts_Click()
>> >> Dim strxlfile As String
>> >> Dim xlapp As Excel.Application
>> >> Dim xlbook As Excel.Workbook
>> >> Dim xlsheet As Excel.Worksheet
>> >> Dim rs As Recordset
>> >>
>> >> strxlfile = "M:excel files\qry_ProgramTotals.xls"
>> >> Set db = CurrentDb
>> >> Set rs = db.OpenRecordset("qry_ProgramTotals", dbOpenSnapshot)
>> >>
>> >> Set xlapp = CreateObject("excel.application")
>> >> With xlapp
>> >> .Visible = True
>> >> .WindowState = xlMinimized
>> >> End With
>> >> Set xlbook = xlapp.Workbooks.Open(strxlfile)
>> >> Set xlsheet = xlbook.Worksheets("Programs Total")
>> >> xlsheet.Cells.Range("a1").Select
>> >> Do Until IsEmpty(ActiveCell)
>> >> ActiveCell.Offset(1, 0).Select
>> >> Loop
>> >> ActiveCell.Offset(1, 0).CopyFromRecordset rs
>> >>
>> >> Set xlapp = Nothing
>> >> Set xlbook = Nothing
>> >> Set xlsheet = Nothing
>> >> rs.Close
>> >> Set rs = Nothing
>> >>
>> >> End Sub
>> >
>> >
>> >
>> > --
>> > TS

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?VFM=?=
Guest
Posts: n/a
 
      10th Feb 2005
Thanks a million. It worked finally.

"Stephen Haley" wrote:

> you are missing 1 line and strictly speaking you should activate the cell
> (select is for a range)
> xlsheet.activate
>
> ie
> xlsheet.Activate
> xlsheet.Range("a1").Activate
> Do Until IsEmpty(ActiveCell)
>
>
> "TS" <(E-Mail Removed)> wrote in message
> news:69F4FA1C-1FCF-445D-B03F-(E-Mail Removed)...
> > Thanks for your response. I made the change you suggested in the code and
> > got
> > the error message: Select method of range class failed when the code
> > reached
> > the following: xlsheet.Cells.Range("a1").Select
> >
> > Please help!! I know I am very close to solve this thanks to you.
> >
> > "Stephen Haley" wrote:
> >
> >> You dont say what is wrong but it looks fine except you have offset by
> >> one
> >> additional row as when you exit the loop you are already on an empty cell
> >>
> >> > Do Until IsEmpty(ActiveCell)
> >> > ActiveCell.Offset(1, 0).Select
> >> > Loop
> >> > ActiveCell.Offset(1, 0).CopyFromRecordset rs
> >> you exit the loop on an empty cell then offset again
> >> last line should be
> >> ActiveCell.CopyFromRecordset rs
> >>
> >> but you are better off not moving the cursor
> >> ie
> >> dim x as integer
> >> x=0
> >> Do Until IsEmpty(ActiveCell.Offset(x,0))
> >> x=x+1
> >> Loop
> >> ActiveCell.Offset(x, 0).CopyFromRecordset rs
> >>
> >> also you need to save the file before terminating excel
> >>
> >> xlbook.save
> >>
> >> rgds
> >>
> >> Stephen
> >>
> >> "TS" <(E-Mail Removed)> wrote in message
> >> news:11EA6F7B-89B5-4147-B3E7-(E-Mail Removed)...
> >> > hi all,
> >> > I only have one row of record in an access query that
> >> > I want to append to an excel sheet. That means I want the code to loop
> >> > through the rows in Excel till finding an empty row and copy the access
> >> > recordset into it. I posted this request before, but haven't gotten a
> >> > correct
> >> > answer yet. The code I used is the following. Please help
> >> >
> >> > Private Sub cmd_ChildrenCountCharts_Click()
> >> >> Dim strxlfile As String
> >> >> Dim xlapp As Excel.Application
> >> >> Dim xlbook As Excel.Workbook
> >> >> Dim xlsheet As Excel.Worksheet
> >> >> Dim rs As Recordset
> >> >>
> >> >> strxlfile = "M:excel files\qry_ProgramTotals.xls"
> >> >> Set db = CurrentDb
> >> >> Set rs = db.OpenRecordset("qry_ProgramTotals", dbOpenSnapshot)
> >> >>
> >> >> Set xlapp = CreateObject("excel.application")
> >> >> With xlapp
> >> >> .Visible = True
> >> >> .WindowState = xlMinimized
> >> >> End With
> >> >> Set xlbook = xlapp.Workbooks.Open(strxlfile)
> >> >> Set xlsheet = xlbook.Worksheets("Programs Total")
> >> >> xlsheet.Cells.Range("a1").Select
> >> >> Do Until IsEmpty(ActiveCell)
> >> >> ActiveCell.Offset(1, 0).Select
> >> >> Loop
> >> >> ActiveCell.Offset(1, 0).CopyFromRecordset rs
> >> >>
> >> >> Set xlapp = Nothing
> >> >> Set xlbook = Nothing
> >> >> Set xlsheet = Nothing
> >> >> rs.Close
> >> >> Set rs = Nothing
> >> >>
> >> >> End Sub
> >> >
> >> >
> >> >
> >> > --
> >> > TS
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Append crosstab result to a table =?Utf-8?B?Y3JhZnR5?= Microsoft Access Queries 2 12th Feb 2007 05:44 AM
Combine crosstab and append query =?Utf-8?B?UmVuw6k=?= Microsoft Access VBA Modules 1 21st Jun 2006 05:21 PM
Append query result to EXCEL file =?Utf-8?B?VFM=?= Microsoft Access VBA Modules 2 2nd Feb 2005 02:55 PM
Crosstab append query J. Shrimp, Jr. Microsoft Access Queries 5 14th Dec 2004 04:27 AM
Re: Result of crosstab query Vega Microsoft Access Queries 1 4th Oct 2003 09:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:52 PM.