Numeric Index of RS.field from field name? 
Author Message
 Numeric Index of RS.field from field name?

Greetings,

I'm wondering if anyone has a quick/dirty way to retrieve
a recordset field's numeric index given that field's name?
(I'm dumping the RS into an array using GetRows for faster
iterations through it, so I'll no longer be able to get at the
fields by their names -- thus the need for a conversion.)

Here's an untested snippet that shows basically what I'm after,
but I have to imagine there's a faster way to do this:

Function IndexFromName(Rs As ADODB.Recordset, FieldName As String) As Integer
        For i = 0 To Rs.Fields.Count - 1
            If Rs.Fields(i).Name = FieldName Then
                IndexFromName = i
                Exit Function
            End If
        Next
    'shouldn't get here unless FieldName not found in fields
    Err.Raise vbObjectError + 2654, "Field '" & FieldName & "' not found in collection"
End Function

Many thanks...



Sat, 03 Jan 2004 23:28:11 GMT  
 Numeric Index of RS.field from field name?
A field object has no index property

As long as the table layout does not change, it is easy to connect every
Column Name with an index. Replace every reference to a column name in your
code through an index by which you address your array.

Daniel

Quote:
----- Original Message -----

Newsgroups: microsoft.public.data.ado,microsoft.public.vb.database.ado
Sent: Tuesday, July 17, 2001 5:28 PM
Subject: Numeric Index of RS.field from field name?

> Greetings,

> I'm wondering if anyone has a quick/dirty way to retrieve
> a recordset field's numeric index given that field's name?
> (I'm dumping the RS into an array using GetRows for faster
> iterations through it, so I'll no longer be able to get at the
> fields by their names -- thus the need for a conversion.)

> Here's an untested snippet that shows basically what I'm after,
> but I have to imagine there's a faster way to do this:

> Function IndexFromName(Rs As ADODB.Recordset, FieldName As String) As
Integer
>         For i = 0 To Rs.Fields.Count - 1
>             If Rs.Fields(i).Name = FieldName Then
>                 IndexFromName = i
>                 Exit Function
>             End If
>         Next
>     'shouldn't get here unless FieldName not found in fields
>     Err.Raise vbObjectError + 2654, "Field '" & FieldName & "' not found
in collection"
> End Function

> Many thanks...



> Greetings,

> I'm wondering if anyone has a quick/dirty way to retrieve
> a recordset field's numeric index given that field's name?
> (I'm dumping the RS into an array using GetRows for faster
> iterations through it, so I'll no longer be able to get at the
> fields by their names -- thus the need for a conversion.)

> Here's an untested snippet that shows basically what I'm after,
> but I have to imagine there's a faster way to do this:

> Function IndexFromName(Rs As ADODB.Recordset, FieldName As String) As
Integer
>         For i = 0 To Rs.Fields.Count - 1
>             If Rs.Fields(i).Name = FieldName Then
>                 IndexFromName = i
>                 Exit Function
>             End If
>         Next
>     'shouldn't get here unless FieldName not found in fields
>     Err.Raise vbObjectError + 2654, "Field '" & FieldName & "' not found
in collection"
> End Function

> Many thanks...



Sat, 03 Jan 2004 23:48:42 GMT  
 Numeric Index of RS.field from field name?


Quote:
> Greetings,

> I'm wondering if anyone has a quick/dirty way to retrieve
> a recordset field's numeric index given that field's name?

I am sure that the Recordset has some internal function that does this but
it is not exposed.
I would dump the properties of the fields collection for each field and see
if there is an undocumented property that gives you this.

Stephen Howe



Sun, 04 Jan 2004 00:55:18 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. rs!field vs. rs.fields(fieldname).value

2. Assign a numeric variable to a numeric field name

3. Creating text box name based on rs.fields(x).name

4. Creating text box name based on rs.fields(x).name

5. remove index from field or delete a indexed field

6. using rs field names

7. ADO: format rs!xxxxx when the field name exists in more than one table

8. How to convert a Yes/No field to a numeric field

9. adodb connection to excel: recordset.fields(i).value=null for fields with numeric value

10. convert text field to numeric field being used in Selection Formula

11. adodb connection to excel: recordset.fields(i).value=null for fields with numeric value

12. adodb connection to excel: recordset.fields(i).value=null for fields with numeric value

 

 
Powered by phpBB® Forum Software