Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default How to populate Excel Range from Access RecordSet?

I have code that loops through about 40 Access mdbs and sucks in a bunch of
data into one internal Access table. The challenge now is getting that data
out of Access and into an Excel Worksheet. I've tried
DoCmd.TransferSpreadsheet - not the solution I need.

This is the kind of thing I'm looking for:

xlapp.Workbooks(strXlsFile).Worksheets(j + 1).CopyFromRecordset rst
xlapp.Workbooks(strXlsFile).Worksheets(j + 1).CurrentRegion.Columns.AutoFit

I want to dump whatever is in the rst into a blank Worksheet.

Can someone help me with syntax or other suggestions?

Thanks!

Here's more complete code if you're interested:

For Each fldSub In fld.SubFolders
strSheetName = fldSub.Name
strMdbPath = fld & "\" & strSheetName & "\MEAN.MDB"
If LinkTable(strMdbPath, strXlsPath) Then
db.Execute strSql, dbFailOnError
Set rst = db.OpenRecordset("tblMean")
j = xlapp.Workbooks(strXlsFile).Worksheets.Count

xlapp.Workbooks(strXlsFile).Worksheets.Add(After:= xlapp.Workbooks _
(strXlsFile).Worksheets(j)).Name = strSheetName
xlapp.Workbooks(strXlsFile).Worksheets(j + 1).CopyFromRecordset
rst
xlapp.Workbooks(strXlsFile).Worksheets(j +
1).CurrentRegion.Columns.AutoFit
k = k + 1
End If
xlapp.Workbooks(strXlsFile).Save
Next fldSub

It took me a while to figure out this syntax:

xlapp.Workbooks(strXlsFile).Worksheets.Add(After:= xlapp.Workbooks(strXlsFile
).Worksheets(j)).Name = strSheetName

but it seems to be working now


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,173
Default How to populate Excel Range from Access RecordSet?

Deko

Why is TransferSpreadsheet no good?

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
HIS


"deko" wrote in message
m...
I have code that loops through about 40 Access mdbs and sucks in a bunch of
data into one internal Access table. The challenge now is getting that
data
out of Access and into an Excel Worksheet. I've tried
DoCmd.TransferSpreadsheet - not the solution I need.

This is the kind of thing I'm looking for:

xlapp.Workbooks(strXlsFile).Worksheets(j + 1).CopyFromRecordset rst
xlapp.Workbooks(strXlsFile).Worksheets(j +
1).CurrentRegion.Columns.AutoFit

I want to dump whatever is in the rst into a blank Worksheet.

Can someone help me with syntax or other suggestions?

Thanks!

Here's more complete code if you're interested:

For Each fldSub In fld.SubFolders
strSheetName = fldSub.Name
strMdbPath = fld & "\" & strSheetName & "\MEAN.MDB"
If LinkTable(strMdbPath, strXlsPath) Then
db.Execute strSql, dbFailOnError
Set rst = db.OpenRecordset("tblMean")
j = xlapp.Workbooks(strXlsFile).Worksheets.Count

xlapp.Workbooks(strXlsFile).Worksheets.Add(After:= xlapp.Workbooks _
(strXlsFile).Worksheets(j)).Name = strSheetName
xlapp.Workbooks(strXlsFile).Worksheets(j + 1).CopyFromRecordset
rst
xlapp.Workbooks(strXlsFile).Worksheets(j +
1).CurrentRegion.Columns.AutoFit
k = k + 1
End If
xlapp.Workbooks(strXlsFile).Save
Next fldSub

It took me a while to figure out this syntax:

xlapp.Workbooks(strXlsFile).Worksheets.Add(After:= xlapp.Workbooks(strXlsFile
).Worksheets(j)).Name = strSheetName

but it seems to be working now




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default How to populate Excel Range from Access RecordSet?

Why is TransferSpreadsheet no good?

Well, for one thing, the path to my Excel workbook is more than 64
characters - this blows up the function. Also, I need to keep the Worksheet
a particular name. If I use TransferSpreadsheet, the worksheet is
automatically renamed the to the name of object exported. (I suppose I
could change it back, though.) I'm also thinking an rst will give me more
control.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default How to populate Excel Range from Access RecordSet?

I'm testing this, but there must be a way to dump the entire rst without
looping (?)

Do Until rst.EOF
For i = 0 To rst.Fields.Count - 1
CurrentField = rst(i)
Sheet.cells(j, i + 1).Value = CurrentField
Next i
rst.MoveNext
j = j + 1
Loop


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default How to populate Excel Range from Access RecordSet?

If I build a Recordset from a 2-column table or query, then this should dump
each column out to the worksheet, is this correct?

Do While Not rst.EOF
For m = 0 To rst.Fields.Count - 1
varCurrentField = rst(m)
xlapp.Workbooks(strXlsFile).Worksheets(j + 1).Cells(n, m + 1).Value
= varCurrentField
Next m
rst.MoveNext
n = n + 1
Loop

This is barfing with: Error Number 1004: "Application-defined or
object-defined error"




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,173
Default How to populate Excel Range from Access RecordSet?

Deko

I can't test the 64 char limit as mine are less than that, but why not save
it to the root and then move it with VBA. If you are using the 'Range'
property in TransferSpreadsheet and using a version of Excel after 5 it will
add a new worksheet to the workbook specified and the name in the Range
property will be the name of the worksheet.

Does that help at all.

If not we may need to look at ODBC or ADODB or similar. Equally, if the end
product is Excel we could easily bring it in through VBA in Excel also.
(Pull rather than push)

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
HIS


"deko" wrote in message
m...
Why is TransferSpreadsheet no good?


Well, for one thing, the path to my Excel workbook is more than 64
characters - this blows up the function. Also, I need to keep the
Worksheet
a particular name. If I use TransferSpreadsheet, the worksheet is
automatically renamed the to the name of object exported. (I suppose I
could change it back, though.) I'm also thinking an rst will give me more
control.





  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default How to populate Excel Range from Access RecordSet?

I can't test the 64 char limit as mine are less than that, but why not
save
it to the root and then move it with VBA. If you are using the 'Range'
property in TransferSpreadsheet and using a version of Excel after 5 it

will
add a new worksheet to the workbook specified and the name in the Range
property will be the name of the worksheet.

Does that help at all.

If not we may need to look at ODBC or ADODB or similar. Equally, if the

end
product is Excel we could easily bring it in through VBA in Excel also.
(Pull rather than push)


Thanks for the reply. Saving to root and then moving the file might well be
a solution. But I've went ahead and used a Recordset for now. It's kind of
slow. I'll bet TransferSpreadsheet is quicker. In any case, the task at
hand is to get the charts looking better. If you care to offer any comments
on this chart code, that would be great.

'===== populate worksheet with data ====='
Set rst = db.OpenRecordset("ExcelData")
Do While Not rst.EOF
For m = 0 To rst.Fields.Count - 1
varCurrentField = rst(m)
xlapp.Workbooks(strXlsFile).Worksheets(j + 1).Cells(n, m + 1).Value
= varCurrentField
Next m
rst.MoveNext
n = n + 1
Loop

'===== add chart ====='
xlapp.Workbooks(strXlsFile).Charts.Add.Name = strSheetName & " Chart"
xlapp.Workbooks(strXlsFile).ActiveChart.ChartType = xlLineMarkers
'*** I think this next line needs help ***
xlapp.Workbooks(strXlsFile).ActiveChart.SetSourceD ata Source:= _
xlapp.Workbooks(strXlsFile).Worksheets(j + 1).Cells(1, 3),
PlotBy:=xlColumns
xlapp.Workbooks(strXlsFile).ActiveChart.HasLegend = False
xlapp.Workbooks(strXlsFile).ActiveChart.ApplyDataL abels
Type:=xlDataLabelsShowValue

(All this code is contained within a loop - approx 40 worksheets and charts
need to be generated)


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Repost! Excel multiple worksheet populate from recordset B[_3_] Excel Programming 1 November 6th 04 10:56 AM
Help! Excel multiple worksheet populate from recordset B[_3_] Excel Programming 1 November 5th 04 09:59 AM
Acquiring a single recordset from Access into Excel Humsel Excel Programming 2 September 1st 04 07:04 AM
How to populate a multi-column activeX listbox on a spreadsheet with an ADO recordset quartz Excel Programming 1 May 3rd 04 10:13 PM
open range (within workbook) as ado recordset - excel vba S. Daum Excel Programming 0 July 25th 03 04:45 PM


All times are GMT +1. The time now is 10:38 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"