Connecting Tech Pros Worldwide Help | Site Map

For Loop Through Recordset

!TG
Guest
 
Posts: n/a
#1: Jul 22 '05
I currently use Do while loop, but I'd rather use a For Loop though I
have never gotten the hang of them.
Would some one please be so kind as to show me how to loop through a
recordset.
Bob Barrows [MVP]
Guest
 
Posts: n/a
#2: Jul 22 '05

re: For Loop Through Recordset


!TG wrote:[color=blue]
> I currently use Do while loop, but I'd rather use a For Loop though I
> have never gotten the hang of them.
> Would some one please be so kind as to show me how to loop through a
> recordset.[/color]

Why would you rather use a For loop?

Anyways, looping through a recordset may not be the most efficient way for
you to do what you need to do. See here for alternatives:
http://www.aspfaq.com/show.asp?id=2467

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


!TG
Guest
 
Posts: n/a
#3: Jul 22 '05

re: For Loop Through Recordset


Bob Barrows [MVP] wrote:[color=blue]
> !TG wrote:
>[color=green]
>>I currently use Do while loop, but I'd rather use a For Loop though I
>>have never gotten the hang of them.
>>Would some one please be so kind as to show me how to loop through a
>>recordset.[/color]
>
>
> Why would you rather use a For loop?
>
> Anyways, looping through a recordset may not be the most efficient way for
> you to do what you need to do. See here for alternatives:
> http://www.aspfaq.com/show.asp?id=2467
>
> Bob Barrows[/color]
For practice sake
McKirahan
Guest
 
Posts: n/a
#4: Jul 22 '05

re: For Loop Through Recordset


"!TG" <2734567183@southwestfunding.com> wrote in message
news:ewISu3DeFHA.3880@tk2msftngp13.phx.gbl...[color=blue]
> Bob Barrows [MVP] wrote:[color=green]
> > !TG wrote:
> >[color=darkred]
> >>I currently use Do while loop, but I'd rather use a For Loop though I
> >>have never gotten the hang of them.
> >>Would some one please be so kind as to show me how to loop through a
> >>recordset.[/color]
> >
> >
> > Why would you rather use a For loop?
> >
> > Anyways, looping through a recordset may not be the most efficient way[/color][/color]
for[color=blue][color=green]
> > you to do what you need to do. See here for alternatives:
> > http://www.aspfaq.com/show.asp?id=2467
> >
> > Bob Barrows[/color]
> For practice sake[/color]

Use GetRows (with a For Loop) instead of looping through a RecordSet:

http://www.learnasp.com/advice/whygetrows.asp


Dave Anderson
Guest
 
Posts: n/a
#5: Jul 22 '05

re: For Loop Through Recordset


!TG wrote:[color=blue]
> I currently use Do while loop, but I'd rather use a For Loop though I
> have never gotten the hang of them.
> Would some one please be so kind as to show me how to loop through a
> recordset.[/color]

Like this?

for (var Employees=[]; !RS.EOF; RS.MoveNext()) {
Employees.push({
LastName:RS.Fields("LastName").Value,
FirstName:RS.Fields("FirstName").Value,
Address:RS.Fields("Address").Value,
Phone:RS.Fields("Phone").Value,
SSN:RS.Fields("SSN").Value
})
}


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.


Bob Barrows [MVP]
Guest
 
Posts: n/a
#6: Jul 22 '05

re: For Loop Through Recordset


!TG wrote:[color=blue]
> Bob Barrows [MVP] wrote:[color=green]
>> !TG wrote:
>>[color=darkred]
>>> I currently use Do while loop, but I'd rather use a For Loop though
>>> I have never gotten the hang of them.
>>> Would some one please be so kind as to show me how to loop through a
>>> recordset.[/color]
>>
>>
>> Why would you rather use a For loop?
>>
>> Anyways, looping through a recordset may not be the most efficient
>> way for you to do what you need to do. See here for alternatives:
>> http://www.aspfaq.com/show.asp?id=2467
>>[/color][/color]

If, by "For loop" you mean a "For Each" loop, then you are out of louck. A
recordset does not expose its Records collection (which is not really a
collection - note: there is no "Records" property in a Recordset object) via
the IEnumerable interface, so "For Each" cannot be used to loop through the
records of a recordset the way it can be used to loop through its Fields
collection:

for each fld in rs.Fields
response.write fld.Name & ": " & fld.Value & "<BR>"
next

If you are talking about a "For i=0 to something" loop, then you need to use
a cursortype that supports bookmarks. This is because you need a way to
1. Tell the recordset which record to point to, and
2. Tell the loop to stop at the last record, using the recordcount (which is
only available with static, keyset and dynamic cursors)

Anyways, if you set the cursortype to either 1(keyset), 2(dynamic) or 3
(static), or set the cursorlocation to 3 (adUseClient), guaranteeing that
you will get a static cursor, before you open the recordset, you will
receive a bookmarkable cursor which will allow you to do this:

rows=rs.RecordCount
if rows > 0 then
for i = 1 to rows
rs.AbsolutePosition=i
'do stuff with current record
next
end if

Such cursortypes are more expensive (consume more system resources) than the
default forwardonly cursor. If you are going to loop through a recordset,
then use the simple "Do While Not rs.EOF...Loop" or "Do Until rs.EOF
....Loop" loops - they will be much more efficient. Better yet, use GetRows
or GetString where appropriate.

HTH,
Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


Closed Thread