473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

CopyFromRecordset only Returning 1 Record

I have searched the web and the questions asked on this forum and I have not been able to find an answer to my problem. Could you please help?

I have an ADODB.Recordset that is loading and returns a record count of 22,158 records. I am trying to load these records into Excel without the user having to save the file, so I am using CopyFromRecordset instead of TransferSpreadsheet. The code is working the only problem is it is only transfering the 1st record. I keep trying to change Row 46 to give me the entire recordset but I have not hit on a solution yet. Please Help!

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmd_XprtXls_Click()
  2. On Error GoTo Err_cmd_XprtXls_Click
  3.  
  4.     Dim conn As ADODB.Connection
  5.     Dim stPath As String
  6.     Dim rst As ADODB.Recordset
  7.     Dim sSQL As String
  8.  
  9.     Dim xlApp As Object
  10.     Dim xlWb As Object
  11.     Dim xlWs As Object
  12.     Dim acRng As Variant
  13.     Dim slRow As Integer
  14.  
  15.     ' Set the string to the path of your database
  16.     stPath = CurrentDb.Name
  17.     ' Open connection to the database
  18.     Set conn = New ADODB.Connection
  19.     conn.Provider = "Microsoft.ACE.OLEDB.12.0;" & _
  20.         "Data Source=" & stPath & ";"
  21.     conn.Open
  22.     ' Open recordset
  23.     Set rst = New ADODB.Recordset
  24.     sSQL = sSelect & vbCrLf & sFrom & vbCrLf & sWhere & vbCrLf & sGroupBy & vbCrLf & sHaving & ";"
  25.  
  26.     Debug.Print sSQL
  27.     rst.Open sSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
  28.     rst.MoveLast
  29.     Debug.Print rst.RecordCount '(Returns 22158 as RecordCount)
  30.  
  31.     ' Create an instance of Excel and add a workbook
  32.     Set xlApp = New Excel.Application
  33.     Set xlWb = xlApp.Workbooks.Add
  34.     Set xlWs = xlWb.Worksheets("Sheet1")
  35.  
  36.     ' Display Excel and give user control of Excel's lifetime
  37.     xlApp.Visible = True
  38.     xlApp.UserControl = True
  39.  
  40.     ' Copy field names to the first row of the worksheet
  41.     fldCount = rst.Fields.Count
  42.     For iCol = 1 To fldCount
  43.         xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
  44.     Next
  45.  
  46.     ' Copy the recordset to the worksheet, starting in cell A2
  47.     xlWs.Range("A2").CopyFromRecordset rst
  48.     ' Auto-fit the column widths and row heights
  49.     xlApp.Selection.CurrentRegion.Columns.AutoFit
  50.     xlApp.Selection.CurrentRegion.Rows.AutoFit
  51.  
  52.     ' Close ADO objects
  53.     rst.Close
  54.     conn.Close
  55.     Set rst = Nothing
  56.     Set conn = Nothing
  57.  
  58.     ' Release Excel references
  59.     Set xlWs = Nothing
  60.     Set xlWb = Nothing
  61.  
  62.     Set xlApp = Nothing
May 16 '13 #1

✓ answered by Rabbit

I see no reason why it wouldn't work. Unless it's because you did a MoveLast... It's just a shot in the dark but try doing a MoveFirst right before your CopyFromRecordset.

7 12127
Oralloy
988 Expert 512MB
time2hike,

Perhaps you need to specify a range large enough to take the entire recordset, not just one cell?

Oralloy
May 16 '13 #2
I am not sure how to specify a larger range. I thought that by leaving the end of the range empty it would expand to the size of my recordset. If the range was the problem wouldn't I get only the value in A2?

My original code for line 47 was
Expand|Select|Wrap|Line Numbers
  1. xlWs.Cells(2, 1).CopyFromRecordset rst
This code returned the same 1 record. I tried making the 1 in the Cells(2,1) = rst.RecordCount but it gave me an error. Everything I have seen says this code should work. I appreciate any help you can provide.
May 16 '13 #3
Rabbit
12,516 Expert Mod 8TB
CopyFromRecordset requires that you specify a range large enough to hold all the data. The Cells collection will only return a range of one cell. Use the Range method instead.
May 16 '13 #4
I changed the Range.
Expand|Select|Wrap|Line Numbers
  1. ' Copy the recordset to the worksheet, starting in cell A2
  2.     xlWs.Range("A2", "S250").CopyFromRecordset rst
And I am still getting 1 record. I was expecting 250 of my recordset to export to Excel. Help!
May 16 '13 #5
Rabbit
12,516 Expert Mod 8TB
I see no reason why it wouldn't work. Unless it's because you did a MoveLast... It's just a shot in the dark but try doing a MoveFirst right before your CopyFromRecordset.
May 16 '13 #6
Thank you Rabbit! The Move First was the Answer.

I have included the final code that works and exports all the records I need exported below for anyone else who is having this problem.

Expand|Select|Wrap|Line Numbers
  1.     rst.Open sSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
  2.     If rst.BOF And rst.EOF Then
  3.         MsgBox "No Data Available to Export", vbOKOnly, "No Data"
  4.         Exit Sub
  5.     End If
  6.     rst.MoveLast
  7.  
  8.     Debug.Print rst.RecordCount
  9.     iRow = rst.RecordCount + 1
  10.  
  11.     ' Create an instance of Excel and add a workbook
  12.     Set xlApp = New Excel.Application
  13.     Set xlWb = xlApp.Workbooks.Add
  14.     Set xlWs = xlWb.Worksheets("Sheet1")
  15.  
  16.     ' Display Excel and give user control of Excel's lifetime
  17.     xlApp.Visible = True
  18.     xlApp.UserControl = True
  19.  
  20.     ' Copy field names to the first row of the worksheet
  21.     fldCount = rst.Fields.Count
  22.     For iCol = 1 To fldCount
  23.         xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
  24.     Next
  25.  
  26.     rst.MoveFirst
  27.  
  28.     ' Copy the recordset to the worksheet, starting in cell A2
  29.     xlWs.Range("A2", "XFD" & iRow).CopyFromRecordset rst
  30.  
  31.     ' Auto-fit the column widths and row heights
  32.     xlApp.Selection.CurrentRegion.Columns.AutoFit
  33.     xlApp.Selection.CurrentRegion.Rows.AutoFit
  34.  
May 16 '13 #7
Rabbit
12,516 Expert Mod 8TB
Glad you got it working.

On a side note, you may want to include an additional check in there to see if the number of rows goes over the max in Excel. I don't know how big your recordset can get but it's something you should be aware of.
May 16 '13 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: news | last post by:
Hi there, Looking for a way of returning the Xth to Yth records of an Oracle database I thought using ROWNUM. Here is the solution I came up with. SELECT * FROM ( SELECT ROWNUM RECID, t.*,
0
by: A E | last post by:
Hi, I was wondering if I have a function that returns a record datatype, and I call it using the as keyword to specify the table structure, can I return a different structure than the one...
3
by: Sarah | last post by:
I know there have been plenty of posts online about this issue, but I have yet to find a solution. I am desperate for a good answer. The issue is: with newly-built PCs and, as it happens, our web...
0
by: Tim | last post by:
I'm trying to work with the Index Server in ASP.Net and am having issues with returning FileIndex. My select statement looks like this: SELECT FileIndex, DocTitle, vpath, filename, size, write,...
2
by: al | last post by:
Greetings, I'm wondering if Excel object CopyFromRecordset is still supported in VB.NET?? If not, what is the alternative, looping through dataset???? MTIA, Grawsha
0
by: Shujun Huang | last post by:
Hi, I am working on converting Informix database to Postgre. I have one question for fetching records using PostgreSQL. The record I am fetching is a variable size text string. Before fetching...
5
by: Robert Fitzpatrick | last post by:
Can someone point me to some more information or perhaps show an example of returning a recordset from a plpgsql function. I'd like to send an argument or arguments to the function, do some queries...
3
by: Kevin | last post by:
Am very new at ASP.NET 2.0 and .NET! Want to get the MembershipID from an inserted record using SqlDataSource:- <InsertParameters> <asp:Parameter Name="MembershipID" Direction="Output"...
3
by: Cor Pruim | last post by:
I have a very strange problem. I have written a Windows Service with VS2003 in vb.net. This service does some calculations and after that it needs to produce some Excel reports by getting data from...
1
by: il0postino | last post by:
Apologies in advance for this newbie question! I have an Access form with an unbound embedded Excel chart on it(Called, OLEUnbound39) (Done on Access form by Insert > Object > Microsoft excel...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.