The code below is from a project that outputs a lot of data from recordsets to Excel. When I run this piece of code and various others I get the error message:
Run-time error '-2147221080 (800401a8)'
Method of 'Cells' of object '_Worksheet' failed
I've tried copying the code into another module and it still didn't work. The thing is this code and all the others that aren't working used to work fine. It seems related to the use of .Cells() however there are other modules that are very similar to this one that work fine. Also the headings (commented as "'Input headings") come out in Excel and then Access freezes.
Please Help??!!
Here is the code with the large sql statement removed:
Dim sqlStat, sqlstat2, strquote As String
Dim rst, rst2 As DAO.Recordset
Dim db As DAO.Database
Dim row, idcomp, prevcount As Integer
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Set db = CurrentDb()
strquote = Chr$(34)
Set objXL = New Excel.Application
objXL.Visible = True
Set objWkb = objXL.Workbooks.Add()
Set objSht = objXL.Worksheets.Add()
'FOR BREIVITY EDITED OUT THE SQL STATEMENT
row = 1
'input headings
objSht.Cells(row, 1).Value = "FirstName"
objSht.Cells(row, 2).Value = "FamilyName"
objSht.Cells(row, 3).Value = "ClientID"
objSht.Cells(row, 4).Value = "FamilyID"
objSht.Cells(row, 5).Value = "Contacts"
Set rst = db.OpenRecordset(sqlStat)
row = 2
Do While Not rst.EOF
If idcomp = rst.Fields(0) And prevcount < rst.Fields(3) Then
objSht.Cells(row - 1, 5).Value = rst.Fields(3)
prevcount = rst.Fields(3)
rst.MoveNext
Else:
objSht.Cells(row, 1).Value = rst.Fields(1)
objSht.Cells(row, 2).Value = rst.Fields(2)
objSht.Cells(row, 3).Value = rst.Fields(0)
objSht.Cells(row, 5).Value = rst.Fields(3)
'get the client's familyid
sqlstat2 = "SELECT FamilyID FROM Family WHERE ClientID = " & rst.Fields(0) & " "
sqlstat2 = sqlstat2 & "AND Family.StartDate <= " & "#" & Now & "# "
sqlstat2 = sqlstat2 & "AND Family.FinishDate >= " & "#" & Now & "#"
Set rst2 = db.OpenRecordset(sqlstat2)
objSht.Cells(row, 4).Value = rst2.Fields(0)
row = row + 1
idcomp = rst.Fields(0)
prevcount = rst.Fields(3)
rst.MoveNext
End If
Loop
'Autofit the column widths to the data in them
objSht.Columns("A:Z").EntireColumn.AutoFit
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set db = Nothing
Set rst = Nothing
Set rst2 = Nothing
Run-time error '-2147221080 (800401a8)'
Method of 'Cells' of object '_Worksheet' failed
I've tried copying the code into another module and it still didn't work. The thing is this code and all the others that aren't working used to work fine. It seems related to the use of .Cells() however there are other modules that are very similar to this one that work fine. Also the headings (commented as "'Input headings") come out in Excel and then Access freezes.
Please Help??!!
Here is the code with the large sql statement removed:
Dim sqlStat, sqlstat2, strquote As String
Dim rst, rst2 As DAO.Recordset
Dim db As DAO.Database
Dim row, idcomp, prevcount As Integer
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Set db = CurrentDb()
strquote = Chr$(34)
Set objXL = New Excel.Application
objXL.Visible = True
Set objWkb = objXL.Workbooks.Add()
Set objSht = objXL.Worksheets.Add()
'FOR BREIVITY EDITED OUT THE SQL STATEMENT
row = 1
'input headings
objSht.Cells(row, 1).Value = "FirstName"
objSht.Cells(row, 2).Value = "FamilyName"
objSht.Cells(row, 3).Value = "ClientID"
objSht.Cells(row, 4).Value = "FamilyID"
objSht.Cells(row, 5).Value = "Contacts"
Set rst = db.OpenRecordset(sqlStat)
row = 2
Do While Not rst.EOF
If idcomp = rst.Fields(0) And prevcount < rst.Fields(3) Then
objSht.Cells(row - 1, 5).Value = rst.Fields(3)
prevcount = rst.Fields(3)
rst.MoveNext
Else:
objSht.Cells(row, 1).Value = rst.Fields(1)
objSht.Cells(row, 2).Value = rst.Fields(2)
objSht.Cells(row, 3).Value = rst.Fields(0)
objSht.Cells(row, 5).Value = rst.Fields(3)
'get the client's familyid
sqlstat2 = "SELECT FamilyID FROM Family WHERE ClientID = " & rst.Fields(0) & " "
sqlstat2 = sqlstat2 & "AND Family.StartDate <= " & "#" & Now & "# "
sqlstat2 = sqlstat2 & "AND Family.FinishDate >= " & "#" & Now & "#"
Set rst2 = db.OpenRecordset(sqlstat2)
objSht.Cells(row, 4).Value = rst2.Fields(0)
row = row + 1
idcomp = rst.Fields(0)
prevcount = rst.Fields(3)
rst.MoveNext
End If
Loop
'Autofit the column widths to the data in them
objSht.Columns("A:Z").EntireColumn.AutoFit
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set db = Nothing
Set rst = Nothing
Set rst2 = Nothing