Results 1 to 7 of 7

Thread: [RESOLVED] Quickest way to locate a record within a dataset

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    17

    [RESOLVED] Quickest way to locate a record within a dataset

    Hi,

    I would like to know what the quickest way is to return the record number of a record within a dataset.

    i am trying to locate the record by using a column that will be holding unique data but is not the records primary key.

    At the moment i am filling the dataset and then using the rows.count method to get the amount of records. Then using a for i = 0 to count-1 loop to check the condition i am looking for. when it meets the criteria the record number will equal the variable i (where i jump out of the for next loop)allowing me to access that record using dataset.tables(0).rows(i)("fieldname") for the fields within the record

    Now whilst this works fine i am currently only dealing with quite small amounts of data and was wondering if there was a better/quicker way of doing it.

    Thanks alot
    Last edited by CptCarter; Jul 27th, 2005 at 04:12 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Quickest way to locate a record within a dataset

    You can get the actual row using the Select method of the DataTable class, which returns an array of DataRows that match an SQL WHERE clause, e.g.:
    Code:
    Dim myRows As DataRow() = myTable.Select("Name LIKE 'a%'")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    17

    Re: Quickest way to locate a record within a dataset

    Thanks for the reply.

    That does look like a quick way to get the record i would need.

    However if i make changes to some of the fields within that record how could i move the changes back to the original record within the dataset without having to loop through the dataset to find the record number.

    Cheers

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Quickest way to locate a record within a dataset

    Should be automagic... I don't think it actualy makes a copy of anything, just references to the original record. So by changing the fields in myRows, it should be automagicaly done in the parent dataset. At least in theory that's how I would expect it happen... haven't had the opportunity to actualy try it.

    Tg
    * 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??? *

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Quickest way to locate a record within a dataset

    As techgnome says. The Select method gets an array of references to the actual DataRows themselves. Just like any reference-type objects, you can have as many references to the same DataRow as you like. This fact is probably the reason that the rows are returned in an array and not a table, because a DataRow can only belong to one DataTable at a time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    17

    Resolved Re: Quickest way to locate a record within a dataset

    Thanks for the replies.

    That should prove alot quicker than the method i was using before.

  7. #7
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Re: [RESOLVED] Quickest way to locate a record within a dataset

    but with this array of data, how can I get the absolute row position of this data row? For example, my controls are not bound, and I've edited the data for the field by which the table was ordered. Now, of course the order changes for all, so I removed the table and reloaded it (i'm sure there's a faster way), and now, having stored the primary key value for the edited row in a variable, I want to jump to that row to populate my controls again
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

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