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 CopyFromRecords et instead of TransferSpreads heet. 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!
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 CopyFromRecords et instead of TransferSpreads heet. 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!
Code:
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
Comment