Sorry about that. The Qry_NormalAssignments provides the 4 employee names for the outer Do loop. Each employee gets their own spreadsheet. The inner do loop grabs their filtered data from Qry_ObjectiveValidationSample and then it dumps it to a spreadsheet, sorts based on the sorting number, and saves the spreadsheet. The first pass through, the first assessor's spreadsheet works fine, but the second pass through fails with the above referenced error. I stripped out the part that fills the data into the spreadsheet because it still fails the same way without data in there.
Code:
Private Sub Btn_Assessor_Samples_Click()Dim SQLxFilter As String
Dim vaData As Variant
Dim rst As ADODB.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Qry_NormalAssignments")
rs.MoveFirst
Do While Not rs.EOF
xAssessor = rs.Fields(0).value
MsgBox xAssessor, vbOKOnly
'Get Recordset
Set rst = CurrentProject.Connection.Execute("Select * from Qry_ObjectiveValidationSample WHERE ASSESSOR =" & Chr(34) & xAssessor & Chr(34))
If rst.BOF Then
Else
'Count records
NumOfRecords = 0
rst.MoveFirst
Do While Not rst.EOF
NumOfRecords = NumOfRecords + 1
rst.MoveNext
Loop
'Set up Assessor Excel Workbook
Dim oExcel As Object
Dim oExcelWrkBk As Object
Dim oExcelWrSht As Object
Dim iCols As Integer
Dim SpecificRecord As Integer
Dim xSort As Integer
Set oExcel = CreateObject("Excel.Application")
oExcel.ScreenUpdating = True
oExcel.Visible = True
Set oExcelWrkBk = oExcel.Workbooks.Add()
Set oExcelWrSht = oExcelWrkBk.Sheets(1)
oExcelWrSht.Cells(1, 1).value = "Family"
oExcelWrSht.Cells(1, 2).value = "Requirement Number"
oExcelWrSht.Cells(1, 3).value = "Requirement"
oExcelWrSht.Cells(1, 4).value = "Objective Number"
oExcelWrSht.Cells(1, 5).value = "Objective Text"
oExcelWrSht.Cells(1, 6).value = "Objective Validation"
oExcelWrSht.Cells(1, 7).value = "Sorting"
With oExcelWrkBk.ActiveSheet.Range("1:1")
.EntireRow.Font.Bold = True
.WrapText = True
End With
With oExcelWrkBk.ActiveSheet.Range("A:A")
.EntireColumn.ColumnWidth = 8
End With
With oExcelWrkBk.ActiveSheet.Range("B:B")
.EntireColumn.ColumnWidth = 12
.WrapText = True
End With
With oExcelWrkBk.ActiveSheet.Range("C:C")
.EntireColumn.ColumnWidth = 30
.WrapText = True
End With
With oExcelWrkBk.ActiveSheet.Range("D:D")
.EntireColumn.ColumnWidth = 9
.WrapText = True
End With
With oExcelWrkBk.ActiveSheet.Range("E:E")
.EntireColumn.ColumnWidth = 30
.WrapText = True
End With
With oExcelWrkBk.ActiveSheet.Range("F:F")
.EntireColumn.ColumnWidth = 70
.WrapText = True
End With
xRow = 2
Columns("A:G").Sort key1:=Range("G:G"), order1:=xlAscending, Header:=xlYes
Columns("G:G").Delete
oExcelWrkBk.SaveAs ("C:\temp\Excel_" & xAssessor & ".xlsx")
MsgBox "data exported", vbOKOnly
oExcel.Quit
rst.Close
Set rst = Nothing
Set oExcelWrSht = Nothing
Set oExcelWrkBk = Nothing
Set oExcel = Nothing
End If
rs.MoveNext
Loop
End Sub