I have an Access database with code that opens an Excel file and then updates information in it. I am getting the error 1004 when I try to put data into some of the cells. I know that the code has the correct worksheet referenced because I am able to get one cell to work, and then the subsequent cells generate errors. I am certain I have the correct cell references in my code.
What is strange is that I have opened the workbook and tried using the immediate window in Excel's VB Editor and I get the same error message. I am able to get code like line 8 to work in the immediate window but not line 7.
I really need this code to run without errors, even if it will populate the data as desired. This is the first of several procedures that manipulate this file.
Any assistance would be appreciated! Thanks!
Code:
1 Set oWS = oWB.Sheets("StartUp")
'populate basic info on worksheet
2 sql = "SELECT MainPolicy.CodeKey, MainPolicy.NamedInsured, MainPolicy.DateEffective, MainPolicy.PolicyNumber " & _
"From MainPolicy WHERE (((MainPolicy.CodeKey)=" & lngCK & "));"
3 Set db = CurrentDb
4 Set rs = db.OpenRecordset(sql)
5 If Not rs.EOF Then
6 oWS.Range("C3") = rs!NamedInsured '<-- works just fine
7 oWS.Range("C4") = rs!DateEffective '<-- generates error
8 oWS.Range("C5") = rs!PolicyNumber '<-- generates error
9 End If
10 rs.Close
What is strange is that I have opened the workbook and tried using the immediate window in Excel's VB Editor and I get the same error message. I am able to get code like line 8 to work in the immediate window but not line 7.
Code:
activeworkbook.Worksheets("StartUp").range("C4").select
activecell.Value="4/15/15" 'this line changes the value in the cell, but generates an error too
activecell.value = #4/15/15# 'this line changes the value in the cell, but generates an error too
activeworkbook.Worksheets("StartUp").range("C5").select
activecell.Value="9339A462"
I really need this code to run without errors, even if it will populate the data as desired. This is the first of several procedures that manipulate this file.
Any assistance would be appreciated! Thanks!