473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to know if loop contains a fixed number of iterations

124 100+
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:

Expand|Select|Wrap|Line Numbers
  1.         strRstClass = "SELECT C.* FROM StdDegReqClass C INNER JOIN StdDegReqDisc D ON C.StdDegReqDiscId = " _
  2.                     & "D.StdDegReqDiscId WHERE C.StdDegReqDiscId = " & lngDiscId & " ORDER BY C.SortOrder"
  3.         Set rstClass = CurrentDb.OpenRecordset(strRstClass, dbOpenSnapshot)
  4.  
or

Expand|Select|Wrap|Line Numbers
  1. Set rstDegReqDisc = CurrentDb.OpenRecordset("SELECT * FROM DegReqDisc WHERE DegReqId = " & lngDegReqId, dbOpenSnapshot)
  2.  
The recordset type is always Snapshot. (If I do any updating/deleting I usually do a CurrentDb.Execute "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:

Expand|Select|Wrap|Line Numbers
  1.         For intClass = 0 To rstClass.RecordCount
  2.  
  3.             If rstClass.EOF Then
  4.                 Exit For
  5.             End If
  6.  
  7.             If sngDiscCredits >= rstDisc!CreditMin And lngDiscCourses >= rstDisc!CourseMin Then
  8.                 lngTakenDiscId = lngDiscId
  9.                 Exit For
  10.             End If
  11.  
  12.             'more code....
  13.             rst.MoveNext
  14.         Next
  15.  
So, from what I described, which looping structure should I use?
May 21 '10 #1
1 2228
ADezii
8,834 Expert 8TB
Personally, this is the Looping Structure that I always use and find to be very concise, and efficient:
Expand|Select|Wrap|Line Numbers
  1. 'The Recordset rst has previously been defined
  2.  
  3. With rst
  4.   Do While Not .EOF
  5.     'processing example
  6.     Debug.Print ![FirstName] & " " & ![LastName]
  7.       .MoveNext
  8.   Loop
  9. End With
  10.  
  11. rst.Close
  12. Set rst = Nothing
May 21 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Michael | last post by:
Hi, I am using w2000+sp4 server with IIS running, how can I know my IIS version number, is it iis5.0 or 5.1 or 6.0? I must know this, since I want to download patches from MS web site,...
10
by: Aaron Smith | last post by:
If I have a datagrid and the bound data file only have 4 rows in it, the grid will show the 4 rows. However, there is blank space below that until it reaches the bottom of the grid. Is there a way...
18
by: Michel Rouzic | last post by:
I can't think of a simple way to print integers with a fix number of digits, in order to obtain something like 0001 for 1 and 0100 for 100 automatically just by specifying in a variable the number...
0
by: Kiran_Juikar | last post by:
Hi, My application needs to be installed on machines across the network. But I want to restrict these installations to a fixed number provided with license file. How should I do it.
4
by: Jack E Leonard | last post by:
I'm looping through the keys and values of a form submission using foreach($_POST as $key => $value) echo "$key = $value<br>"; No problems there. All works as expected. But seveal of the...
0
by: pomeroymiles | last post by:
I have a pivotchart which I cause to take a huge list of people's test percentiles and calculate the average for each year. I then display the average above each bar. The problem is that there are...
2
by: raulverde | last post by:
I have an element declared like this : <xs:element name="class"> <xs:complexType> <xs:sequence> <xs:element name="name" type="className" minOccurs="0" /> <xs:element name="students"...
0
by: sysmanint1 | last post by:
I am a total neophyte at Visual Basic but found the following post and reply from Clint concerning a dynamic range. Also, have never "posted" to a discussion I have made a macro that works on...
19
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.