Results 1 to 6 of 6

Thread: Recordset and referencing a field [Resolved]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739

    Recordset and referencing a field [Resolved]

    I am using an ADO recordset. Now there are several ways to reference a field within the recordset. Is there any difference in the way you do it? Is one way better than the other?

    VB Code:
    1. rs.fields("Field 1")
    2. rs("Field 1")
    3.  
    4. rs(0) 'I think this way would be the fastest since it doesn't
    5. 'have to do any comparison of the names in the fields. It knows
    6. 'to go straight to the first field in the recordset
    7.  
    8. 'there is one more way using the ! but I forget the syntax

    I was just wondering if any method is better.
    Last edited by Maldrid; Jul 21st, 2003 at 03:52 PM.

  2. #2
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Generally speaking you don't want to view fields by index number - makes your code much less readable and could potentially break it/render it useless should the field order change. Your first two examples are the same, the field property is just the default property of the rs object - as .text is to the textbox control. The other method is
    VB Code:
    1. rs!Field1

  3. #3
    Fanatic Member
    Join Date
    May 2002
    Posts
    746

    Also...

    From one of my MS training texts:

    "The most efficient technique is to reference the field name directly. You can use the Fields collection, which is more explicit code, but is less efficient."
    VB Code:
    1. 'most efficient
    2. txtFirstName.Text = rs!First_Name
    3.  
    4. 'explicit, but less efficient
    5. txtFirstName.Text = rs.Fields("First_Name").Value

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    There is no way of using the more efficient way if you have spaces in your fields is there?

    VB Code:
    1. 'This can not be done since there is a space
    2. rs!Field Name
    3. 'Obviously this can't be done either since syntax is wrong
    4. rs!"Field Name"

    The only way I could fix this is if I re-name the fields using underscores instead of spaces.

    Thanks for your replies, I will note it in the future.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by Maldrid
    There is no way of using the more efficient way if you have spaces in your fields is there?

    VB Code:
    1. 'This can not be done since there is a space
    2. rs!Field Name
    3. 'Obviously this can't be done either since syntax is wrong
    4. rs!"Field Name"

    The only way I could fix this is if I re-name the fields using underscores instead of spaces.

    Thanks for your replies, I will note it in the future.
    VB Code:
    1. 'This can not be done since there is a space
    2. rs!Field Name
    3. 'Obviously this can't be done either since syntax is wrong
    4. rs!"Field Name"
    5.  
    6. 'But this works....
    7. rs![Field Name]
    I usually use the .Fields("[Field Name]").value method....
    that way if the fields get moved aorund in the query (happens quite often)... then things don't get hoarked .... as would happen if I used the .Fields(intFldNum).Value....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Ah ok, thats great. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width