Cycle though fields of a recordset

S

Steve

What is the code to cycle through the fields of a recordset starting at the
second field through the last field when the index of the last field is not
expliciyly known?

Thanks for all help!

Steve
 
T

Terry Kreft

Your looking for Fields.Count

For lngX = 1 to rs.Fields.Count -1
' do stuff ...
Next

The fields collection is a zero based collection.
Therefore
Field 2 is at index 1
Field n is at index (.Count -1)

Terry
 
A

Allen Browne

This example uses a loop counter so you can start where you wish:

Function CycleFields()
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim i As Integer

Set rs = DBEngine(0)(0).OpenRecordset("SELECT * FROM MyTable;")
For i = 0 To rs.Fields.Count - 1
Set fld = rs.Fields(i)
Debug.Print fld.Name, fld.Type
Next
Set fld = Nothing
rs.Close

Set rs = Nothing
End Function
 
S

Steve

Allen,

Thanks for taking the time to provide the whole code. As always,I appreciate
your help!

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top