Hi all,
Been working on some code that I found on this Forum and I'm have a few problems with it. I want to export multiple records to specific columns in an excel spreadsheet.
The code I have so far:
The original Sub heading was
But this shows up as red in Access 2003
I referenced Excel object library 11.0 to eliminate the application errors.
When I apply the code. It will open the spreadsheet, but it will not go any further and comes up with and error:
"Application-defined or Object-defined Error"
Any help would be appreciated
Cheers
Dean
-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
Been working on some code that I found on this Forum and I'm have a few problems with it. I want to export multiple records to specific columns in an excel spreadsheet.
The code I have so far:
Code:
Public Sub ExportQuery()
On Error GoTo err_Handler
'Excel object variables
Dim appExcel As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim sTemplate As String
Dim sTempFile As String
Dim sOutput As String
Dim counter
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim sSQL As String
Dim IRecords As Long
Dim iRow As Integer
Dim iCol As Integer
Dim iFld As Integer
sOutput = CurrentProject.Path & "\RosterTemplate.xls"
'Create the Excel Application, Workbook and Worksheet and Database object
Set appExcel = New Excel.Application
appExcel.Visible = True
Set wbk = appExcel.Workbooks.Open(sOutput)
sSQL = "SELECT * FROM tblSample"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
If Not rst.BOF Then
rst.MoveFirst
counter = 1
Do While Not rst.EOF
With wbk
.Sheets("Week1,2").Cells(counter, "B1") = rst.Fields("AppointmentDate")
.Sheets("Week1,2").Cells(counter, "B2") = rst.Fields("AppointmentDesc")
.Sheets("Week1,2").Cells(counter, "B4") = rst.Fields("AppointmentTime")
.Sheets("Week1,2").Cells(counter, "B5") = rst.Fields("MC")
.Sheets("Week1,2").Cells(counter, "B6") = rst.Fields("Who")
.Sheets("Week1,2").Cells(counter, "B7") = rst.Fields("LeadVox")
.Sheets("Week1,2").Cells(counter, "B8") = rst.Fields("Vox1")
.Sheets("Week1,2").Cells(counter, "B9") = rst.Fields("vox2")
.Sheets("Week1,2").Cells(counter, "B10") = rst.Fields("vox3")
.Sheets("Week1,2").Cells(counter, "B11") = rst.Fields("vox4")
.Sheets("Week1,2").Cells(counter, "B12") = rst.Fields("vox5")
.Sheets("Week1,2").Cells(counter, "B13") = rst.Fields("vox6")
.Sheets("Week1,2").Cells(counter, "B14") = rst.Fields("piano")
.Sheets("Week1,2").Cells(counter, "B15") = rst.Fields("keys1")
.Sheets("Week1,2").Cells(counter, "B16") = rst.Fields("keys2")
.Sheets("Week1,2").Cells(counter, "B17") = rst.Fields("LGtr")
.Sheets("Week1,2").Cells(counter, "B18") = rst.Fields("RGtr")
.Sheets("Week1,2").Cells(counter, "B19") = rst.Fields("AccGtr")
.Sheets("Week1,2").Cells(counter, "B20") = rst.Fields("Bass")
.Sheets("Week1,2").Cells(counter, "B21") = rst.Fields("sax")
.Sheets("Week1,2").Cells(counter, "B22") = rst.Fields("Drums")
.Sheets("Week1,2").Cells(counter, "B23") = rst.Fields("FOH")
.Sheets("Week1,2").Cells(counter, "B24") = rst.Fields("SndStg")
.Sheets("Week1,2").Cells(counter, "B25") = rst.Fields("Light")
.Sheets("Week1,2").Cells(counter, "B26") = rst.Fields("LightAss")
.Sheets("Week1,2").Cells(counter, "B27") = rst.Fields("Graphic")
.Sheets("Week1,2").Cells(counter, "B28") = rst.Fields("vision")
.Sheets("Week1,2").Cells(counter, "B29") = rst.Fields("cam1")
.Sheets("Week1,2").Cells(counter, "B30") = rst.Fields("cam2")
.Sheets("Week1,2").Cells(counter, "B31") = rst.Fields("rec")
.Sheets("Week1,2").Cells(counter, "B32") = rst.Fields("Items")
.Sheets("Week1,2").Cells(counter, "B33") = rst.Fields("Songlist")
' repeat these statements for each field
counter = counter + 1
End With
rst.MoveNext
Loop
rst.Close
exit_Here:
'Cleanup all objects (resume next on errors)
Set wbk = Nothing
appExcel.Quit
Set appExcel = Nothing
Set rst = Nothing
Set dbs = Nothing
Exit Sub
err_Handler:
MsgBox "Error is " & Err.Description
Resume exit_Here
End If
End Sub
The original Sub heading was
Code:
Public Sub ExportQuery() as String
But this shows up as red in Access 2003
I referenced Excel object library 11.0 to eliminate the application errors.
When I apply the code. It will open the spreadsheet, but it will not go any further and comes up with and error:
"Application-defined or Object-defined Error"
Any help would be appreciated
Cheers
Dean
-------------------------------------------------------------
"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."