Solved

how to increment a recordset ?

Posted on 2001-07-18
14
290 Views
Last Modified: 2011-10-03
hi there....


I have this

RS!f1
RS!f2
RS!f3
RS!f4
RS!f5
etc............

how can I do to use a loop that automaticly increment the recordset ?

like :

for i = 1 to 10
 res = RS!f(i)
next i

(this doesn't work of curse...)

Regards to you all VB helpers ;o)



 



0
Comment
Question by:albundy
  • 4
  • 3
  • 3
  • +3
14 Comments
 
LVL 5

Expert Comment

by:rkot2000
ID: 6295125
option 1

dim x as field (dao or ado)

for each x in rs.fields

debug.print x.value
next

or

dim i as long

for i = 0 to rs.fields.count-1


0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6295127
.movenext
0
 
LVL 5

Expert Comment

by:rkot2000
ID: 6295130
option 1

dim x as field (dao or ado)

for each x in rs.fields

debug.print x.value
next

or

dim i as long

for i = 0 to rs.fields.count-1
 debug.print rs.fields(i).value
next i

sorry for previous message  ...
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6295135
Do while not RS.EOF
   res = RS!f(i)
   RS.MoveNext
loop
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6295275
It is a little unclear to understand precisely what you're looking for.



If your recordset contains several fields , 10 of which are literally named "F1", "F2", "F3", etc., you can use code similiar to the following:

   Dim idxField As Integer

   For idxField = 1 To 10
      Debug.Print rs.Fields("F" & CStr(idxField))
   Next 'idxField




If you are wanting to cycle through *all* the fields in the recordset, regardless of their names, then you can loop through the Fields collection of the recordset with either a numeric index, or by using the For-Each statement (as has been suggested by someone earlier):

   Dim idxField As Integer

   For idxField = 0 To rs.Fields.Count - 1
      Debug.Print rs.Fields(idxField)
   Next 'idxField


   ' *** OR ***


   Dim Fld As Field

   For Each Fld In rs.Fields
      Debug.Print Fld
   Next 'Fld
   Set Fld = Nothing



-Dennis Borg
0
 

Expert Comment

by:wmike
ID: 6295339
dim rst as recordset
rst.movefirst
do while not rst.eof
   rst.movenext
loop
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6295367
wmike, thanks for reposting my suggestion!  I thought it was good also  :)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 6

Accepted Solution

by:
VK earned 50 total points
ID: 6295558
For i = 1 To 10
    res = RS("F" & i)
Next i
:-)
0
 
LVL 5

Expert Comment

by:rkot2000
ID: 6295568
I don?t care about points, but people can post a different solution?
Dave and I posted solutions other solutions just the same.
If you don?t know better or different why ?.?
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6295797
rkot2000:

>I don?t care about points, but people can post a
>different solution?
>Dave and I posted solutions other solutions just the same.
>If you don?t know better or different why ?.?


I can tell you *why* I posted what I did.

First, I do not post the same answer as someone else. Sometimes, however, I'll read a question and start composing an answer. Meanwhile, someone else also starts composing an answer and then submits their answer before I click on the Submit button. Now, I have no way of knowing that anyone else has also submitted a reply, let alone *what* they said in their reply, until I click on Submit. Only *then* do I see what was submitted while I was typing my own reply. If my answer then happens to be strikingly similiar or identical to someone else's, it is purely coincidential.

Secondly, I may submit a post giving my endorsement of someone else's answer. I sometimes do this when there are various of suggestions and where many of them have problems. I'll then point out the solution I believe the person should use, and I'll explain *why*.


As to why I posted what I did in *THIS* thread, the following comments should explain:

Many of the answers were extremely brief, even to the extent that it may not be helpful to the person asking the question.

   For example, look at Dave Greene's first response:
   (Dave Greene, please note that this is not a personal attack)
----------------------------------
      .movenext
----------------------------------

This reply does not say much; and, depending upon AlBundy's experience level, he may not have a clue as to what ".movenext" is supposed to mean, or how it is to be used.

A bit of dialog, or explanation, even if brief, is often at least as important as the solution itself.

Another thing to note is the fact that I believe that AlBundy was looking for a means of cycling thru the FIELDS, and not cycling thru the RECORDS.



Dave Greene's second reply was quite a bit better, but there is a problem with it:

----------------------------------
      Do while not RS.EOF
        res = RS!f(i)
        RS.MoveNext
      loop
----------------------------------

'f' is not a property or collection of a Recordset Object. This would produce an error. I *think* Dave was trying to compose the name of the field (e.g. "F1", "F2", etc), but I don't know for sure.

Also, one is left to wonder how the variable ' i ' is defined, and from where it obtains its value.


rkot, your comments were fine, although "Option 1" really ought to be "Option Base 1"; but this is a minor problem. Your solution was fine, assuming that AlBundy wanted to cycle through *ALL* fields in the recordset.

But upon reading AlBundy's question, it was not absolutely clear (at least not to me) whether he needed to cycle through *all* fields, or just the ones whose names started with a common prefix (ex: F1, F2, F3, etc); although I suspect it's the latter.

For those reasons, and also due to (what I feel to be) the lack of adequate explaination, I posted my comments. I posted what I considered to be a complete answer, covering both possibilities (all fields, or only certain ones).

I'm sure you noted that I included an acknowlegement that someone else suggested For loops (where I had said "as has been suggested by someone earlier")



I also do not care about the points; but I also feel that the questioner deserves some explaination. I also wanted to make AlBundy aware that he was a little vague as to his objective.

My objective is not to get the points, but to help the person know what the solution is, and also *why* and *how* it works.

This is why I posted my comments.


-Dennis Borg
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6295820
All good points Dennis, no offense taken.
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6295849
Dave:

>All good points Dennis, no offense taken.

Thank-you, Dave! Incidently, I went to school with a Dave Green.

-Dennis Borg
0
 

Expert Comment

by:wmike
ID: 6296830
Yeah, you're right.
Sorry about that Dave.
0
 

Author Comment

by:albundy
ID: 6297205
hi

thx to all of you that responded to me :)

anyway the most clear and working code is from VK.

I give you the point pal.

as I can't give point to all of you... I please to not be angry about me ;)

Regards,

Oliver
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now