jason_kelly
Board Regular
- Joined
- Jul 8, 2010
- Messages
- 50
Hi There,
I need your help with fixing the following code below.
Whenever I go to export my Data to Excel it seems to work fine, however,
When there is a larger Recordset (205 records total), it fails with error message:
------------------------------------------------------
Run-time error '-2147467259 (800040005)':
Method 'CopyFromRecordset' of object 'Range' failed.
------------------------------------------------------
I am stuck and don't know how to rid this error.
Any help is greatly appreciated.
Much thanks and appreciation
Cheers,
Jay
I need your help with fixing the following code below.
Whenever I go to export my Data to Excel it seems to work fine, however,
When there is a larger Recordset (205 records total), it fails with error message:
------------------------------------------------------
Run-time error '-2147467259 (800040005)':
Method 'CopyFromRecordset' of object 'Range' failed.
------------------------------------------------------
I am stuck and don't know how to rid this error.
Any help is greatly appreciated.
Much thanks and appreciation
Cheers,
Jay
Code:
'-------------------------------------------------------
Public Sub ExportTOExcel()
'-------------------------------------------------------
Dim oApp As Object
Dim oWB As Object
Dim maxRows
Dim curRecs
Dim FullFileName
Dim tempdate
Set oApp = CreateObject("Excel.Application")
oApp.Visible = False
Set oWB = oApp.Workbooks.Add
temp_date = Format(Date, "dd/mm/yyyy") & " " & Format(Now, "h:mm AMPM")
If Val(oApp.Version) < 12 Then
FullFileName = Application.GetSaveAsFilename("Export.xls", _
"Excel file (*.xls),*.xls", 1, frmSplash.IMTS_Caption & " - Export to")
maxRows = 65000
Else
FullFileName = Application.GetSaveAsFilename("Export.xlsx", _
"Excel file (*.xlsx),*.xlsx", 1, frmSplash.IMTS_Caption & " - Export to")
maxRows = 1048576
End If
If FullFileName <> False Then
If recCount > maxRows Then
loops = recCount / maxRows
Else
loops = 1
End If
curRecs = maxRows
' Get the Headers
ReDim hdrs(rs.Fields.Count)
x = 0
For Each fld In rs.Fields
hdrs(x) = fld.Name
x = x + 1
Next fld
oApp.ActiveSheet.Cells.EntireRow.RowHeight = 11
oApp.ActiveSheet.Cells.EntireRow.Font.Name = tahoma
oApp.ActiveSheet.Cells.EntireRow.Font.Size = 8
oWB.Sheets(1).Cells(1, 1) = "IMTS Report"
oWB.Sheets(1).Cells(1, 1).Font.Bold = True
oWB.Sheets(1).Range("A1:Q1").Merge
oWB.Sheets(1).Range("A1:Q1").HorizontalAlignment = xlCenter
oWB.Sheets(1).Cells(2, 1) = "Date: " & temp_date
oWB.Sheets(1).Range("A2:C2").Merge
oWB.Sheets(1).Range("A2:C2").HorizontalAlignment = xlLeft
oWB.Sheets(1).Range("A4:Q4").Interior.ColorIndex = 15
For i = 0 To rs.Fields.Count - 1
oWB.Sheets(1).Cells(4, i + 1).Value = rs.Fields(i).Name
Next
For i = 1 To loops
oWB.Sheets(1).Range("4:3").Font.Bold = True
oWB.Sheets(1).Cells(5, 1).CopyFromRecordset rs, maxRows
curRecs = curRecs + maxRows
If i <> loops Then
Set oWB = oWB.Worksheets.Add
oWB.Name = shtName & i + 1
End If
Next i
oWB.SaveAs (FullFileName)
oWB.Close
Set oWB = Nothing
oApp.Quit
Set oApp = Nothing
Else
Exit Sub
End If
End Sub