Numeric Index of RS.field from field name?

Numeric Index of RS.field from field name?

Post by Paul Smit » Thu, 19 Jul 2001 00:28:11



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...

 
 
 

Numeric Index of RS.field from field name?

Post by Daniel Webe » Thu, 19 Jul 2001 00:48:42


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

----- 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...


 
 
 

Numeric Index of RS.field from field name?

Post by Stephen How » Thu, 19 Jul 2001 01:55:18



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

 
 
 

1. 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...

2. ERROR:DB-Library Process Dead - Connection Broken

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

4. SQL Server 2000--Accessing SQL Server Using a URL

5. how could I get the source table name and field name from a view field

6. Database Question

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

8. Identify gaps in identity column

9. What is ODBC equivalent of rs.fields.().name

10. remove index from field or delete a indexed field

11. One-field INDEX performance VS multiple fields index

12. Can't access Paradox files with numeric field names

13. Create a primary key with a numeric field and a character field