how to know if loop contains a fixed number of iterations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bullfrog83
    New Member
    • Apr 2010
    • 124

    #1

    how to know if loop contains a fixed number of iterations

    According to the thread "For...Next vs Do...Loop, and the Winner is?" you should use a For...Next loop if it has a fixed number of iterations and doesn't contain any logical conditions. This may be a dumb question but how do I know if my loop contains a fixed number of iterations? The majority of my loops consist of opening a recordset with a string expression equating to a query. Example:

    Code:
            strRstClass = "SELECT C.* FROM StdDegReqClass C INNER JOIN StdDegReqDisc D ON C.StdDegReqDiscId = " _
                        & "D.StdDegReqDiscId WHERE C.StdDegReqDiscId = " & lngDiscId & " ORDER BY C.SortOrder"
            Set rstClass = CurrentDb.OpenRecordset(strRstClass, dbOpenSnapshot)
    or

    Code:
    Set rstDegReqDisc = CurrentDb.OpenRecordset("SELECT * FROM DegReqDisc WHERE DegReqId = " & lngDegReqId, dbOpenSnapshot)
    The recordset type is always Snapshot. (If I do any updating/deleting I usually do a CurrentDb.Execu te "UPDATE..." , not on the recordset.) I want to loop through the recordset until EOF or until another condition is met if not at EOF. Here's an example of a For...Next loop that I have that illustrates this:

    Code:
            For intClass = 0 To rstClass.RecordCount
            
                If rstClass.EOF Then
                    Exit For
                End If
    
                If sngDiscCredits >= rstDisc!CreditMin And lngDiscCourses >= rstDisc!CourseMin Then
                    lngTakenDiscId = lngDiscId
                    Exit For
                End If
    
                'more code....
                rst.MoveNext
            Next
    So, from what I described, which looping structure should I use?
    Last edited by bullfrog83; May 21 '10, 06:10 PM. Reason: Reworded first paragraph
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Personally, this is the Looping Structure that I always use and find to be very concise, and efficient:
    Code:
    'The Recordset rst has previously been defined
    
    With rst
      Do While Not .EOF
        'processing example
        Debug.Print ![FirstName] & " " & ![LastName]
          .MoveNext
      Loop
    End With
    
    rst.Close
    Set rst = Nothing

    Comment

    Working...