Domino Code Fragment

Code Name*
When Looping Through a ResultSet, While.NextRow Is Not Recommended
Date*
04/28/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.18.222.125.171
Description*
Instead of While.NextRow, use Do at the beginning of the loop and the condition at the end, with a statement
such as "Loop Until res.IsEndOfData."
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:
Problem:
When looping through the records of a ResultSet, it is recommended that you do not use While.NextRow to
start the loop. An infinite loop can result because of the way LotusScript processes errors with relation to
Resume Next and a method such as NextRow, when used in conjunction with "On Error Resume Next."

With the following code, for example, an infinite loop will result if column 13 does not exist:

Dim conn As New ODBCConnection
Dim qry As New ODBCQuery
Dim res As New ODBCResultSet

On Error Goto ErrHandler

If conn.ConnectTo("school data") Then
Set qry.Connection = conn
qry.SQL = "SELECT * FROM salary"
Set res.Query = qry
Call res.Execute()
While.NextRow() 'NextRow returns the first row, the first time through the loop
Messagebox("Row: " & res.CurrentRow & " first col = " & res.GetValue(13))
Wend
Else
Messagebox("Could not connect to server")
End If
Exit Sub
ErrHandler:
Messagebox "An error occurred while running this agent"
Resume Next

Solution:
Instead of While.NextRow, use Do at the beginning of the loop and the condition at the end, with a statement
such as "Loop Until res.IsEndOfData."

For example:

Dim conn As New ODBCConnection
Dim qry As New ODBCQuery
Dim res As New ODBCResultSet

On Error Goto ErrHandler:
If conn.ConnectTo("school data") Then
Set qry.Connection = conn
qry.SQL = "SELECT * FROM salary"
Set res.Query = qry
Call res.Execute()
Do
Call res.NextRow() 'NextRow returns the first row, the first time through the loop
Messagebox("Row: " & res.CurrentRow & " first col = " & res.GetValue(1))
Loop Until res.IsEndOfData
Else
Messagebox("Could not connect to server")
End If
Exit sub
ErrHandler:
Messagebox "An error occurred while running this agent"
Resume Next