I have been struggling with a loop within a loop issue that I can't resolve. What I am trying to do is create a tab in an excel spreadsheet that has one providers summary information in it instead of everyone's. On my first loop I have no issue. It is the second loop that I get the error 3021. I would appreciate any help.
Tom
Tom
Code:
Do
sProv1 = ""
sSQLP = "SELECT p.provname ,p.Last " & _
"FROM DICT_Prov p " & _
"WHERE p.clntid = 41 AND p.provid > 2 " & _
"ORDER BY p.provname;"
Set rstP = CurrentDb.OpenRecordset(sSQLP, dbOpenSnapshot)
If Not rstP.EOF Then
With rstP
.MoveLast
.MoveFirst
End With
sLast = (rstP![Last])
sProv = (rstP![provname])
End If
For P = 1 To rstP.RecordCount
iRw = 6
sSQL = "TRANSFORM Sum(DATA.rvu) AS SumOfrvu " & _
"SELECT Prov.provname " & _
"FROM (((DATA_ProvLessAssists DATA " & _
"INNER JOIN z_ProcessPds Pds ON DATA.clntid=Pds.clntid AND DATA.rptpd=Pds.rptpd) " & _
"INNER JOIN DICT_Prov Prov ON DATA.provid=Prov.provid) " & _
"INNER JOIN DICT_Dpt Dpt ON DATA.dptid=Dpt.dptid) " & _
"WHERE Pds.yrdiff=0 and Prov.provname='" & sProv & "'" & _
" GROUP BY Dpt.dptdesc, Prov.provname, Prov.Last " & _
"ORDER BY Dpt.dptdesc, Prov.provname, Pds.monasdt " & _
"PIVOT Pds.monasdt;"
Set rstLA = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)
If Not rstLA.EOF Then
With rstLA
.MoveLast
.MoveFirst
End With
X = 4
For I = 1 To rstLA.RecordCount
With goXl.ActiveSheet
If sProv1 <> (rstP![provname]) Then X = X + 1
If sProv1 <> (rstP![provname]) Then sSheet = "RVU_LessAssists_" & X
If sProv1 <> (rstLA![provname]) Then goXl.Sheets(sSheet).Select
If sProv1 <> (rstLA![provname]) Then sSheet = "RVU_LessAssists_" & (rstLA![Last])
If sProv1 <> (rstLA![provname]) Then goXl.ActiveSheet.Name = sSheet
If sProv1 <> (rstP![provname]) Then goXl.Sheets(sSheet).Select
If sProv1 <> (rstP![provname]) Then goXl.Sheets(sSheet).Activate
If sProv1 <> (rstP![provname]) Then iRw = 6
goXl.Sheets(sSheet).Range("A6").CopyFromRecordset rstLA
sProv1 = (rstP![provname])
rstLA.MoveNext
End With
Next I
End If 'If Not rstLA.EOF Then
rstP.MoveNext
Next P
Loop Until rstLA.EOF