Hello All,
I am trying to create a generic function that exports the recordset of the current report to Excel. My code is as below:
Public Function ExcelPrint()
Dim rpt As Report
Dim rst As ADODB.Recordset
Dim objExcel As Excel.Application
Dim intRow As Integer
Dim intColumn As Integer
Set rpt = Screen.ActiveReport
Set rst = rpt.Recordset
Set objExcel = New Excel.Application
objExcel.Visible = True
objExcel.SheetsInNewWorkbook = 1
objExcel.Workbooks.Add
With objExcel.ActiveSheet
.Cells(1, 1).Value = rpt.Name
For intColumn = 0 To rst.Fields.Count - 1
.Cells(intRow, intColumn + 1) = rst.Fields(intColumn).Name
Next
intRow = 4
Do Until rst.EOF
For intColumn = 0 To rst.Fields.Count - 1
.Cells(intRow, intColumn + 1) = rst.Fields(intColumn)
Next
intRow = intRow + 1
rst.MoveNext
Loop
End With
objExcel.Close True
objExcel.Quit
Set objExcel = Nothing
End Function
My problem occurs on the line:
Set rst = rpt.Recordset
as you don't seem to be able to access the recordset method for a report like you can for a form.
How do I access a report's recordset? I can't go through the reports recordsource as this would involve redefining the queries parameters which will differ from report to report.
Any ideas?
Thanks in advance.
Robert
I am trying to create a generic function that exports the recordset of the current report to Excel. My code is as below:
Public Function ExcelPrint()
Dim rpt As Report
Dim rst As ADODB.Recordset
Dim objExcel As Excel.Application
Dim intRow As Integer
Dim intColumn As Integer
Set rpt = Screen.ActiveReport
Set rst = rpt.Recordset
Set objExcel = New Excel.Application
objExcel.Visible = True
objExcel.SheetsInNewWorkbook = 1
objExcel.Workbooks.Add
With objExcel.ActiveSheet
.Cells(1, 1).Value = rpt.Name
For intColumn = 0 To rst.Fields.Count - 1
.Cells(intRow, intColumn + 1) = rst.Fields(intColumn).Name
Next
intRow = 4
Do Until rst.EOF
For intColumn = 0 To rst.Fields.Count - 1
.Cells(intRow, intColumn + 1) = rst.Fields(intColumn)
Next
intRow = intRow + 1
rst.MoveNext
Loop
End With
objExcel.Close True
objExcel.Quit
Set objExcel = Nothing
End Function
My problem occurs on the line:
Set rst = rpt.Recordset
as you don't seem to be able to access the recordset method for a report like you can for a form.
How do I access a report's recordset? I can't go through the reports recordsource as this would involve redefining the queries parameters which will differ from report to report.
Any ideas?
Thanks in advance.
Robert