Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Export Recordset to Excel file
Author:
vaibhav
Submitted:
4/3/2007
Version:
VB6
Compatibility:
VB6
Category:
Databases
Views:
29817
Pass CommonDialogBox control & Recordeset Object to ExportTOExcel procedure Complete recordset transfer to Excel file.
Declarations:
'none
Code:
Public Sub ExportTOExcel(ByRef CommDial1 As CommonDialog, ByRef Recordset1 As ADODB.Recordset) On Error GoTo Err: Dim i, j As Integer Dim str1 As String i = 0 j = 0 With CommDial1 .CancelError = True .ShowSave str1 = .FileName End With Dim createExcel As New Excel.Application Dim Wbook As Excel.Workbook Dim Wsheet As Excel.Worksheet Set Wbook = createExcel.Workbooks.Add Set Wsheet = Wbook.Worksheets.Add For i = 0 To Recordset1.Fields.Count - 1 Wsheet.Cells(1, i + 1).Value = Recordset1.Fields(i).Name Next i If (Recordset1.RecordCount > 0) Then Recordset1.MoveFirst For i = 0 To Recordset1.RecordCount - 1 For j = 0 To Recordset1.Fields.Count - 1 Wsheet.Cells(i + 2, j + 1).Value = Recordset1(j).Value Next j Recordset1.MoveNext Next i End If Wbook.SaveAs str1 Wbook.Close True Set createExcel = Nothing Set Wbook = Nothing Set Wsheet = Nothing Set Wbook = createExcel.Workbooks.Open(str1) createExcel.Visible = True Exit Sub Err: Select Case Err.Number Case 32755 MsgBox "Press Cacel button" Case 1004 MsgBox "OverWrite Cancel" Wbook.Close False Case Else MsgBox Err.Number & " " & Err.Description End Select End Sub
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement