alexfwalker81
Member
- Local time
- Today, 11:49
- Joined
- Feb 26, 2016
- Messages
- 93
I'm getting a 424 Object Required error when I try to run this. Attempting to debug by Google, but I can't see where I'm making an error - guessing it's in the syntax around rs.fields.
Code:
Public Function WriteToExcel()
Dim rs
Dim db As DAO.Database
Dim oExcel As Excel.Application
Dim oExcelWrkBk As Excel.Workbook
Dim oExcelWrSht As Excel.Worksheet
Set db = CurrentDb
Set rs = CurrentDb.OpenRecordset("qry_latest_entry")
'Start Excel
On Error Resume Next
Set oExcel = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
On Error GoTo Error_Handler
Set oExcel = CreateObject("Excel.Application")
Else
On Error GoTo Error_Handler
End If
oExcel.ScreenUpdating = False
oExcel.Visible = False 'This is false by default anyway
Set oExcelWrkBk = oExcel.Workbooks.Open("Y:\alextest.xlsx")
Set oExcelWrSht = oExcelWrkBk.Sheets("tbl_BACS_entry")
If rsID.EOF = True Then
Else
Do Until rs.EOF = True
oExcelWrSht.Cells(oExcelWrSht.Rows.Count, 1).End(xlUp).Offset(1, 0) = rs.Fields("ID")
oExcelWrSht.Cells(oExcelWrSht.Rows.Count, 2).End(xlUp).Offset(1, 0) = rs.Fields("DepositType")
oExcelWrSht.Cells(oExcelWrSht.Rows.Count, 3).End(xlUp).Offset(1, 0) = rs.Fields("DateReceived")
oExcelWrSht.Cells(oExcelWrSht.Rows.Count, 4).End(xlUp).Offset(1, 0) = rs.Fields("ChequeDate")
oExcelWrSht.Cells(oExcelWrSht.Rows.Count, 5).End(xlUp).Offset(1, 0) = rs.Fields("ChequeNumber")
oExcelWrSht.Cells(oExcelWrSht.Rows.Count, 6).End(xlUp).Offset(1, 0) = rs.Fields("PayeeName")
oExcelWrSht.Cells(oExcelWrSht.Rows.Count, 7).End(xlUp).Offset(1, 0) = rs.Fields("PayeeReference")
oExcelWrSht.Cells(oExcelWrSht.Rows.Count, 8).End(xlUp).Offset(1, 0) = rs.Fields("AmountDeposited")
oExcelWrSht.Cells(oExcelWrSht.Rows.Count, 9).End(xlUp).Offset(1, 0) = rs.Fields("Comments")
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing
oExcelWrSht.Range("A1").Select
oExcelWrkBk.Save
oExcel.ScreenUpdating = True
oExcel.Visible = False
Exit_Point:
Set oExcelWrSht = Nothing
Set oExcelWrkBk = Nothing
Set oExcel = Nothing
Exit Function
Error_Handler:
MsgBox Err & " - " & Err.Description
GoTo Exit_Point
End Function