473,769 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

For Loop Through Recordset

!TG
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.
Jul 22 '05 #1
5 25087
!TG wrote:
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.


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.
Jul 22 '05 #2
!TG
Bob Barrows [MVP] wrote:
!TG wrote:
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.

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

For practice sake
Jul 22 '05 #3
"!TG" <27********@sou thwestfunding.c om> wrote in message
news:ew******** ******@tk2msftn gp13.phx.gbl...
Bob Barrows [MVP] wrote:
!TG wrote:
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.

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

For practice sake


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

http://www.learnasp.com/advice/whygetrows.asp
Jul 22 '05 #4
!TG wrote:
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.


Like this?

for (var Employees=[]; !RS.EOF; RS.MoveNext()) {
Employees.push( {
LastName:RS.Fie lds("LastName") .Value,
FirstName:RS.Fi elds("FirstName ").Value,
Address:RS.Fiel ds("Address").V alue,
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.
Jul 22 '05 #5
!TG wrote:
Bob Barrows [MVP] wrote:
!TG wrote:
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.

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


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.RecordC ount
if rows > 0 then
for i = 1 to rows
rs.AbsolutePosi tion=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"
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1991
by: Ken Fine | last post by:
I'm trying to include a list of people that's the result of looping through a recordset in a CDONTS mail. I'm trying to Dim the output of a loop, and it ain't working -- I'm getting a syntax error. Any ideas why the following snippet isn't working? <% Dim ConfirmedSpeakerList2 ConfirmedSpeakerList2=(While ((Repeat1__numRows <> 0) AND (NOT rsConfirmedSpeakers.EOF)) (rsConfirmedSpeakers.Fields.Item("Per_GivenName").Value)
0
2783
by: Sue Adams | last post by:
I actually have two issues/questions: I have an autonumber field in an access db table that I grab and later use to update a record in another table withing the same db. The code I use to get it from the db table is: ''Retrieve the Registration Identification Number strRegisterID = Rs("Register_ID") Prior to testing my code and actually updating the db, I''m trying to write it to the page to make sure their isn''t a loop or massive...
10
2116
by: Ronny Sigo | last post by:
Hello all, Could anybody give me the correct syntax for defining a loop using the form's recordset for as long as it is not at the end of the table ? example: Do While not EOF ' this is the part I don't know commands .... ..... ..... Loop
52
6349
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
4
2098
by: keithsimpson3973 | last post by:
Hi. I have a loop that was written for me by some outstanding programmers here (willakawill and killer42). It works great if I only select one record to check for duplicates. The user can pick on one form, 13 areas they want to schedule use on and on another form they do the same and can select all 9 areas. When they select multiple areas to schedule, my application goes into an infinite loop and keeps displaying the conflicts found dialog until...
3
2706
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Here is my loop and it runs fine: ---------------------------------------------------- sSQL = "SELECT * FROM STORE_ITEMS" Set DataRec = DB.execute(sSQL) if not DataRec.EOF then do while not DataRec.EOF SKU = trim (DataRec("SKU")) ITEM_ID = trim(DataRec("ITEM_ID")) ...
4
68735
MMcCarthy
by: MMcCarthy | last post by:
The following code is simply an example of some code that processes through two recordsets. It can be helpful for anyone curious as to how to start processing with recordsets (Which objects to refer to & how to set them up correctly etc), as well as for those who want a quick run-through of some basic multi-recordset logic. Function yourFunctionName() Dim db As DAO.Database Dim rs1 As DAO.Recordset Dim rs2 As DAO.Recordset Set db =...
1
6102
by: hmiller | last post by:
Hey Guys, I'm just playing around with some code I wrote for work and am trying to minimize the coding as much as possible. So two questions: 1. What are your best practices for organizing your code, aside from comments? I typically split my code up into sub procedures and then run them all from the top of the code. My problem is that my code page is
8
6492
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table using a code loop and an INSERT INTO query. About 800,000 records of raw text. Later, I can then loop through and parse these 800,000 strings into usable data using more code. The problem I have is that the conversion of the text file, using a...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.