If I have the following:
How do I reference that to make a for each loop?Code:Dim rs As DAO.Recordset Set rs = CurrentDb.OpenRecordset("SELECT ID, assigned FROM tbl142", dbOpenDynaset)
| SitePoint Sponsor |

If I have the following:
How do I reference that to make a for each loop?Code:Dim rs As DAO.Recordset Set rs = CurrentDb.OpenRecordset("SELECT ID, assigned FROM tbl142", dbOpenDynaset)
<?php//Kyle Wolfeecho devBlog("My Dev Notes");
Code ASP:WHILE NOT (rs.EOF) '-- Your Code Here rs.MoveNext WEND
Loops through the record set while it hasn't reached End Of File. I don't think you need a for each because you can already access the values of the record by name. rs("MyFieldValue")

See I wound up going with this solution by why does everyone use this method rather than for each? That's what that loop is made for...
<?php//Kyle Wolfeecho devBlog("My Dev Notes");


This is really more a VB topic, but
If this were true there would only be one loop construct in VB, not the three or four that exist.That's what that loop is made for...
I think you can use for each on a recordset anyway if you want, but I'm one that always uses do loops on recordsets.
Doug G
=====
"If you ain't the lead dog, the view is always the same - Anon

The three or four that exist in any language have their purpose. For each should do exactly what it sounds like. For each in array. Do while is a true loops that can be done until a certain criteria is established. Yes Do while works here but the "for each" is designed to do exactly what I'm trying to do here, which is to run through every item in an array...
<?php//Kyle Wolfeecho devBlog("My Dev Notes");


An ADO recordset is not an array.Yes Do while works here but the "for each" is designed to do exactly what I'm trying to do here, which is to run through every item in an array...
Doug G
=====
"If you ain't the lead dog, the view is always the same - Anon

How is it not an array? Other than the technical "its not defined as one". It most definitely is a multi demensional array and for each is designed to do a specefic task for each in an array.
<?php//Kyle Wolfeecho devBlog("My Dev Notes");

Technically, it's not an array - it's a collection. A slight difference, but a difference nonetheless.
If you want to get it into an array, then use the getRows method which moves the collection into a two dimensional array.

Ok. Additionally, how can I access the value of a field by its record number? Ie. rs(2)("fieldname")
<?php//Kyle Wolfeecho devBlog("My Dev Notes");

If the array is called arrRecSet, then the values are accessed via
strfieldValue = arrRecSet(fieldID, rowID) where fieldID is based on the order of the fields being selected and the rowID is the row in the recordset array being returned.
If you need the column names being returned, you can use this method: http://www.codefixer.com/articles/getrows.htm

This doesn't work for me. Do I have to convert the recrdset into an array first somehow?
<?php//Kyle Wolfeecho devBlog("My Dev Notes");





Um - that was already said (post #8). Use GetRows() to convert the recordset to a 2-dimensional array.

Yes I see that.
Im getting subscipt out of range errorCode:myArray = rsEmps.GetRows msgBox myArray(1,1)
<?php//Kyle Wolfeecho devBlog("My Dev Notes");

Did you read either of the links I provided you? They both explained the concept of getRows pretty cleanly and had decent code examples.
But to save time, here is the basic code structure I used for this type of activity. It's been a year or two, so the syntax might be a little off, but you should be able to clean those up....
Code ASP:dim strSQL, rs, arrRecSet, recCount dim varField1, varField2, varField3 strSQL = "SELECT field1, field2, field3 FROM tableName" set rs = Server.CreateObject("ADODB.recordset") rs.Open strSQL, conn if rs.EOF and rs.BOF then recCount = -1 else ' Move the values into array and get count arrRecSet = rs.GetRows(-1) : recCount = UBound(arrRecSet(0, 2)) end if ' Loop through entire recordset... for i = 0 to recCount 'move fields into temp readable fields for readability sake set varField1 = arrRecSet(0, i) : set varField2 = arrRecSet(1, i) : set varField3 = arrRecSet(3, i) ' Now do whatever you need to with values.... Response.Write "Field1 = " & varField1 & vbCrLf Response.Write "Field2 = " & varField2 & vbCrLf Response.Write "Field3 = " & varField3 & vbCrLf next

From your code I get invalid arguement on arrRecSet = rs.GetRows(-1)
Also I had one working except it was always skipping over the first row?
<?php//Kyle Wolfeecho devBlog("My Dev Notes");

That line should be fine (I've used it probably 1000 times). There was a typo though.
recCount = UBound(arrRecSet(0, 2)) should be recCount = UBound(arrRecSet, 2)

Not sure but having -1 for getRows arguement just isn't working. Again though. Why is it always skipping the first result? Is that what the -1 is supposed to fix?
<?php//Kyle Wolfeecho devBlog("My Dev Notes");

Nope - the -1 tells it to get all the records.
On your for loop - are you starting from 0 or 1? You need to start from 0.

Yes I am starting from 0. I try to access it directly via 0,0 and still goes to second result
<?php//Kyle Wolfeecho devBlog("My Dev Notes");

I think we need to see your code to help you out further - there's a piece of information there that's causing the problems.
Bookmarks