ayazgreat
03-30-2008, 12:11 AM
Hi
I have received following macro from some one but it does not work when i run my macro to export data to excel because I have 4 or five lacks records in data table when exporting in excel it gives error message and copy incomplete records could you please help to resolve it?
Public Function RecordsetToExcelMod(ByRef ERST As Recordset, _
Optional ByVal SpreadsheetName As String = "Sheet", _
Optional ByVal lMaxRows As Long = 65535)
'Use this to generate an Excel application from an ADO recordset.
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlws As Excel.Worksheet
Dim rstTemp As ADODB.Recordset
Dim rstField As ADODB.Field
Dim iCol As Integer
Dim lRow As Long
Dim iTotSheets As Integer
Dim iShe As Integer
Set xlApp = New Excel.Application
Set xlWb = xlApp.Workbooks.Add
Set rstTemp = New ADODB.Recordset
If lMaxRows > 65535 Then
lMaxRows = 65535
End If
' Copy fields to the temporary recordset
For Each rstField In ERST.Fields
rstTemp.Fields.Append rstField.Name, rstField.Type, rstField.DefinedSize, rstField.Attributes And adFldIsNullable
rstTemp(rstField.Name).Precision = rstField.Precision
rstTemp(rstField.Name).NumericScale = rstField.NumericScale
Next
rstTemp.Open
ERST.MoveFirst
' Calculate how many sheets will be needed
iTotSheets = (ERST.RecordCount / lMaxRows) + 0.5
For iShe = 1 To iTotSheets
' Create additional sheets if necesary
If iShe > 3 Then
' Creates a new sheet after the previous one
xlWb.Worksheets.Add , xlWb.Worksheets(iShe - 1)
End If
Set xlws = xlWb.Worksheets(iShe)
xlws.Activate
xlws.Name = Left(SpreadsheetName & " " & iShe & " of " & iTotSheets, 31)
' Create column names in the spreadsheet
For iCol = 1 To ERST.Fields.count
xlws.Cells(1, iCol).Value = ERST.Fields(iCol - 1).Name
Next
xlApp.Selection.CurrentRegion.Font.Bold = True
' Copy records to temporary recordset.
' Only copy the maximum allowed per sheet
For lRow = 1 To lMaxRows
If ERST.EOF Then
Exit For
Else
rstTemp.AddNew
For iCol = 0 To (ERST.Fields.count - 1)
rstTemp.Fields(iCol).Value = ERST.Fields(iCol).Value
Next
rstTemp.Update
rstTemp.MoveNext
ERST.MoveNext
End If
Next
' CopyFromRecordset is faster then copying row by row to the spreadsheet.
' This is why we use a temporary recordset.
xlws.Cells(2, 1).CopyFromRecordset rstTemp
If rstTemp.RecordCount > 0 Then
rstTemp.MoveFirst
End If
' Empty temporary recordset.
Do While Not rstTemp.EOF
If Not rstTemp.EOF Then
rstTemp.Delete
End If
rstTemp.MoveNext
Loop
rstTemp.UpdateBatch
' Format spreadsheet
xlApp.Selection.CurrentRegion.Columns.AutoFit
xlApp.Selection.CurrentRegion.Rows.AutoFit
Next
xlApp.Visible = True
xlApp.UserControl = True
Set xlws = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
Set rstTemp = Nothing
End Function
I have received following macro from some one but it does not work when i run my macro to export data to excel because I have 4 or five lacks records in data table when exporting in excel it gives error message and copy incomplete records could you please help to resolve it?
Public Function RecordsetToExcelMod(ByRef ERST As Recordset, _
Optional ByVal SpreadsheetName As String = "Sheet", _
Optional ByVal lMaxRows As Long = 65535)
'Use this to generate an Excel application from an ADO recordset.
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlws As Excel.Worksheet
Dim rstTemp As ADODB.Recordset
Dim rstField As ADODB.Field
Dim iCol As Integer
Dim lRow As Long
Dim iTotSheets As Integer
Dim iShe As Integer
Set xlApp = New Excel.Application
Set xlWb = xlApp.Workbooks.Add
Set rstTemp = New ADODB.Recordset
If lMaxRows > 65535 Then
lMaxRows = 65535
End If
' Copy fields to the temporary recordset
For Each rstField In ERST.Fields
rstTemp.Fields.Append rstField.Name, rstField.Type, rstField.DefinedSize, rstField.Attributes And adFldIsNullable
rstTemp(rstField.Name).Precision = rstField.Precision
rstTemp(rstField.Name).NumericScale = rstField.NumericScale
Next
rstTemp.Open
ERST.MoveFirst
' Calculate how many sheets will be needed
iTotSheets = (ERST.RecordCount / lMaxRows) + 0.5
For iShe = 1 To iTotSheets
' Create additional sheets if necesary
If iShe > 3 Then
' Creates a new sheet after the previous one
xlWb.Worksheets.Add , xlWb.Worksheets(iShe - 1)
End If
Set xlws = xlWb.Worksheets(iShe)
xlws.Activate
xlws.Name = Left(SpreadsheetName & " " & iShe & " of " & iTotSheets, 31)
' Create column names in the spreadsheet
For iCol = 1 To ERST.Fields.count
xlws.Cells(1, iCol).Value = ERST.Fields(iCol - 1).Name
Next
xlApp.Selection.CurrentRegion.Font.Bold = True
' Copy records to temporary recordset.
' Only copy the maximum allowed per sheet
For lRow = 1 To lMaxRows
If ERST.EOF Then
Exit For
Else
rstTemp.AddNew
For iCol = 0 To (ERST.Fields.count - 1)
rstTemp.Fields(iCol).Value = ERST.Fields(iCol).Value
Next
rstTemp.Update
rstTemp.MoveNext
ERST.MoveNext
End If
Next
' CopyFromRecordset is faster then copying row by row to the spreadsheet.
' This is why we use a temporary recordset.
xlws.Cells(2, 1).CopyFromRecordset rstTemp
If rstTemp.RecordCount > 0 Then
rstTemp.MoveFirst
End If
' Empty temporary recordset.
Do While Not rstTemp.EOF
If Not rstTemp.EOF Then
rstTemp.Delete
End If
rstTemp.MoveNext
Loop
rstTemp.UpdateBatch
' Format spreadsheet
xlApp.Selection.CurrentRegion.Columns.AutoFit
xlApp.Selection.CurrentRegion.Rows.AutoFit
Next
xlApp.Visible = True
xlApp.UserControl = True
Set xlws = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
Set rstTemp = Nothing
End Function