Hello,
I'm trying to create a series of excel spreadsheets from a recordset object. The recordset contains three records (to test), so I'd like to create three excel spreadsheets. The name of the spreadsheet is comprised of the 'practice' and '.xls'. Effectively, i want to code to loop through the recordset, grab the first practice, create an excel spreadsheet using the practice and splatter the data into it, move to the next record in the recordset and so on.
The code below works as far as one spreadsheet is concerned (i.e. the 'first' record in the recordset), but fails at the 'rst.movenext' with the 'runtime error '3021''.
If remark the following lines:
oSheet.Range("A1").CopyFromRecordset rst
oBook.SaveAs ePath
oExcel.Quit
I get no error. So the error must reside here. I can't see what I'm doing wrong. Can anyone help please?
TIA
I'm trying to create a series of excel spreadsheets from a recordset object. The recordset contains three records (to test), so I'd like to create three excel spreadsheets. The name of the spreadsheet is comprised of the 'practice' and '.xls'. Effectively, i want to code to loop through the recordset, grab the first practice, create an excel spreadsheet using the practice and splatter the data into it, move to the next record in the recordset and so on.
The code below works as far as one spreadsheet is concerned (i.e. the 'first' record in the recordset), but fails at the 'rst.movenext' with the 'runtime error '3021''.
If remark the following lines:
oSheet.Range("A1").CopyFromRecordset rst
oBook.SaveAs ePath
oExcel.Quit
I get no error. So the error must reside here. I can't see what I'm doing wrong. Can anyone help please?
TIA
Code:
Dim rst As DAO.Recordset
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim ePath As String
Dim strSQL As String
Dim rcount As Integer
strSQL = "SELECT Practice, EmailAddress, ImmunisationDate FROM tblTempMail"
Set rst = CurrentDb.OpenRecordset(strSQL)
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
rst.MoveLast
rcount = rst.RecordCount
rst.MoveFirst
Do Until rst.EOF = True
ePath = rst![practice]
ePath = "C:\EmailFromAccess\" & ePath & ".xls"
oSheet.Range("A1").CopyFromRecordset rst
oBook.SaveAs ePath
oExcel.Quit
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
strSQL = ""