In looking at the code, I see many errors and logic bombs........ here are a few of my comments.
This
Code:
rs.MoveFirst
rs.MoveLast 'This is required otherwise you may not get the right count
ICount = rs.RecordCount 'Determine the number of returned records
the above statements should be in the following order
rs.MoveLast 'This is required otherwise you may not get the right count
ICount = rs.RecordCount 'Determine the number of returned records
rs.MoveFirst 'move back to the top of the recordset
'-----------------------------------------------------------
This is an endless loop. "ICount" will never get to zero. "ICount" is an integer but is never decremented.
Code:
With rst400_ReceiptS
Do Until ICount = 0
If ICount = 0 Then Exit Sub
.MoveNext
Loop
End With
'-----------------------------------------------------------
I cut out a lot of the statements to focus on the control statements
Code:
Public Sub xxold()
Set db = CurrentDb()
Set rs = db.OpenRecordset("475_MD01RF01Q01") 'open the recordset for use (table, Query, SQL Stateme
With rs 'W1
If .RecordCount > 0 Then 'Ensure there are records. The next 2 line determine the number of returned records IT1
ICount = rs.RecordCount 'Determine the number of returned records
Do While ICount > 0 '<<-ICount will never get to zero
Set [rst400_ReceiptS] = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
If rst400_ReceiptS.EOF Then Exit Sub 'If the recordset is empty, exit.
'this is a very poor way to exit a sub. All open record sets are not closed and not destroyed
' the dB object is not destroyed either
With rst400_ReceiptS
Do Until ICount = 0 '<<-ICount will never get to zero. "ICount" is an integer but is never decremented.
If ICount = 0 Then Exit Sub '<<-ICount will never get to zero. "ICount" is an integer but is never decremented.
'this is a very poor way to exit a sub. All open recordsets are not closed and not destroyed
' the dB object is not destroyed either
.MoveNext
Loop
End With
Exit Sub
'exits without ensuring all open recordsets are closed and destroyed
' the dB object is not destroyed either
ErrorHandler: 'error handler is inside a loop. Plus, there is no "On Error" statement at the top of the sub
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
Loop
End If 'IT1
End With 'W1
End Sub
Note that in the above code, the recordset "rs" never moves off of the current record (no rs.MoveNext or rs.MovePrevious)