This program has 5 loops. When it goes through the second loop I get the error 91. The second loop has no records in it and that is what I am expecting. I have moved the end if associated with the following code
Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
If Not rst.EOF Then
With rst
.MoveLast
.MoveFirst
End With
to many different spots but I can't get the code to work. What I want to happen is if the If Not rst.EOF Then statement is executed I want the code to execute the rstCPT.MoveNext And the next Y statements. I have tried different methods but I can't seem to get it working. Any help is appreciated.
Tom
Code:
Private Sub cmdXL_Click()
Dim boolErr As Boolean
Dim strSQL As String
Dim strUCI As String
Dim strMnth As String
Dim strCPT As String
Dim strFullCPT As String
Dim rstCPT As Recordset
Dim rst As Recordset
Dim Z As Integer
Dim iRw As Integer
Dim strRefProv As String
Dim rstDat As Recordset
Dim Y As Integer
Dim strRptType As String
Dim strFileType As String
' OPEN EXCEL
Call xlCreate
If goxlPresent = True Then
' Get List of CPT to Process
strSQL = "SELECT cpt,cptdesc FROM [_CPTToProcess] ORDER BY cpt;"
Set rstCPT = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
With rstCPT
.MoveLast
.MoveFirst
End With
For Y = 1 To rstCPT.RecordCount ' Cycle through CPT List
strCPT = (rstCPT![CPT])
strFullCPT = (rstCPT![CPT]) & " - " & (rstCPT![cptdesc])
strSQL = "SELECT impID,uci,monasdt,PatName,AcctNu,doschg,chgamt,amt " & _
"FROM RPT_Export " & _
"WHERE (cptcode = '" & (strCPT) & "') " & _
"ORDER BY PatName,doschg;"
Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
If Not rst.EOF Then
With rst
.MoveLast
.MoveFirst
End With
' **********************
' *** CREATE INVOICE ***
' **********************
strUCI = (rst![uci])
strMnth = (rst![impID])
iRw = 7
' Open Template
With goxl
[Blue] .Workbooks.Open FileName:="\\salmfilesvr1\Public\Client Services\AutoRpts\_RptSets\OTHER\" & (strUCI) & "\Tools\Invoice.xlt" [/Blue]
.Sheets("Invoice").Select
End With
With goxl.ActiveSheet
.Cells(1, 1).Value = strUCI
.Cells(3, 1).Value = (rst![monasdt])
.Cells(5, 1).Value = strFullCPT
End With
For Z = 1 To rst.RecordCount ' Add Referring Providers to Report
With goxl.ActiveSheet
.Cells(iRw, 1).Value = (rst![PatName])
.Cells(iRw, 2).Value = (rst![AcctNu])
.Cells(iRw, 3).Value = (rst![doschg])
.Cells(iRw, 4).Value = (rst![chgamt])
.Cells(iRw, 5).Value = (rst![amt])
End With
' Move to Next Row
iRw = iRw + 1
rst.MoveNext
Next Z
rst.Close
Set rst = Nothing
' Delete Extra Rows
With goxl
.Rows("" & (iRw) & ":1000").Select
.Selection.Delete Shift:=xlUp
.Cells(4, 1).Select
End With
'Save Workbook
strFileType = "_Inv_" & (strCPT) & "_"
strUCI = "PRM"
Call xlSave(strUCI, strFileType)
End If
rstCPT.MoveNext
Next Y
rstCPT.Close
Set rst = Nothing
Else
MsgBox "Can't create Excel Object", vbOKOnly, "Excel not found"
End If
' Close Excel Instance
Call xlKill
' Message it is closed
MsgBox "Referring Report Completed.", , "Done!"
End Sub