I have an Access subroutine that needs to generate an Excel spreadsheet for accounting. I've used late-bound Excel automation before, but for some reason I keep running into a strange issue when I run this one.
This is the relevant portion of the procedure:
When I run this, I always get "Applicatio n-defined error" when I hit ".cells(1, 1).value = rs!ItemNum" - but the crazy part is that, after the code breaks, I can hit "Run" and it continues right through, no problems. Any ideas on why this happens and how to work around it? Google hasn't been very helpful on this.
This is the relevant portion of the procedure:
Code:
Private Sub GenerateExplodedBOM()
'On Error GoTo Err_Handler
Dim appXL As Object
Dim wbBOM As Object
Dim rs As New adodb.Recordset
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True
Set wbBOM = appXL.workbooks.Add
wbBOM.Activate
wbBOM.sheets.Add Before:=wbBOM.sheets(1)
wbBOM.sheets(1).name = Forms!PDC_home!ItemNum & " Exploded BOM"
wbBOM.sheets(1).Select
rs.Open "SELECT * FROM tblProduct WHERE ItemNum = '" & _
Forms!PDC_home!ItemNum & "'", CurrentProject.Connection, _
adOpenForwardOnly
With wbBOM.sheets(1)
.cells(1, 1).value = rs!ItemNum
.cells(1, 2).value = rs!PurchaseDescription
.cells(1, 3).value = "1"
.cells(1, 4).value = CCur(rs!BOMtotal)
End With
rs.Close
Set rs = Nothing
Comment