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! - Private Sub cmd_XprtXls_Click()
-
On Error GoTo Err_cmd_XprtXls_Click
-
-
Dim conn As ADODB.Connection
-
Dim stPath As String
-
Dim rst As ADODB.Recordset
-
Dim sSQL As String
-
-
Dim xlApp As Object
-
Dim xlWb As Object
-
Dim xlWs As Object
-
Dim acRng As Variant
-
Dim slRow As Integer
-
-
' Set the string to the path of your database
-
stPath = CurrentDb.Name
-
' Open connection to the database
-
Set conn = New ADODB.Connection
-
conn.Provider = "Microsoft.ACE.OLEDB.12.0;" & _
-
"Data Source=" & stPath & ";"
-
conn.Open
-
' Open recordset
-
Set rst = New ADODB.Recordset
-
sSQL = sSelect & vbCrLf & sFrom & vbCrLf & sWhere & vbCrLf & sGroupBy & vbCrLf & sHaving & ";"
-
-
Debug.Print sSQL
-
rst.Open sSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
-
rst.MoveLast
-
Debug.Print rst.RecordCount '(Returns 22158 as RecordCount)
-
-
' Create an instance of Excel and add a workbook
-
Set xlApp = New Excel.Application
-
Set xlWb = xlApp.Workbooks.Add
-
Set xlWs = xlWb.Worksheets("Sheet1")
-
-
' Display Excel and give user control of Excel's lifetime
-
xlApp.Visible = True
-
xlApp.UserControl = True
-
-
' Copy field names to the first row of the worksheet
-
fldCount = rst.Fields.Count
-
For iCol = 1 To fldCount
-
xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
-
Next
-
-
' Copy the recordset to the worksheet, starting in cell A2
-
xlWs.Range("A2").CopyFromRecordset rst
-
' Auto-fit the column widths and row heights
-
xlApp.Selection.CurrentRegion.Columns.AutoFit
-
xlApp.Selection.CurrentRegion.Rows.AutoFit
-
-
' Close ADO objects
-
rst.Close
-
conn.Close
-
Set rst = Nothing
-
Set conn = Nothing
-
-
' Release Excel references
-
Set xlWs = Nothing
-
Set xlWb = Nothing
-
-
Set xlApp = Nothing
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
time2hike,
Perhaps you need to specify a range large enough to take the entire recordset, not just one cell?
Oralloy
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 - 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.
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.
I changed the Range. - ' Copy the recordset to the worksheet, starting in cell A2
-
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!
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.
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. - rst.Open sSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
-
If rst.BOF And rst.EOF Then
-
MsgBox "No Data Available to Export", vbOKOnly, "No Data"
-
Exit Sub
-
End If
-
rst.MoveLast
-
-
Debug.Print rst.RecordCount
-
iRow = rst.RecordCount + 1
-
-
' Create an instance of Excel and add a workbook
-
Set xlApp = New Excel.Application
-
Set xlWb = xlApp.Workbooks.Add
-
Set xlWs = xlWb.Worksheets("Sheet1")
-
-
' Display Excel and give user control of Excel's lifetime
-
xlApp.Visible = True
-
xlApp.UserControl = True
-
-
' Copy field names to the first row of the worksheet
-
fldCount = rst.Fields.Count
-
For iCol = 1 To fldCount
-
xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
-
Next
-
-
rst.MoveFirst
-
-
' Copy the recordset to the worksheet, starting in cell A2
-
xlWs.Range("A2", "XFD" & iRow).CopyFromRecordset rst
-
-
' Auto-fit the column widths and row heights
-
xlApp.Selection.CurrentRegion.Columns.AutoFit
-
xlApp.Selection.CurrentRegion.Rows.AutoFit
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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.*,
|
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...
|
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...
|
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,...
|
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
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |