Results 1 to 9 of 9

Thread: Recordset Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Location
    Philadelphia, PA
    Posts
    120

    Recordset Question

    Hello!

    VB Code:
    1. rs.Open "SELECT * FROM Customers", cn, adOpenKeyset, adLockOptimistic

    The recordset opens just fine and allows me to .Addnew
    Also, rs.EOF is false

    So why does rs.Recordcount return a -1 when that recordset actually contains records?

    Could it be some permissions problem with my SQL server SA account?

    Thanks in advance!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Recordset Question

    The 'default' is -1, which means the recordcount is unknown at the moment.

    There are ways to get it to work, but that slows things down a bit, so it is best to avoid it unless you really need it (generally using EOF does the job just as well).

  3. #3
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Recordset Question

    Quote Originally Posted by si_the_geek
    The 'default' is -1, which means the recordcount is unknown at the moment.

    There are ways to get it to work, but that slows things down a bit, so it is best to avoid it unless you really need it (generally using EOF does the job just as well).
    In my experiments I haven't found EOF to be any faster but my ADO code is probably not that great anyway.

    To the original poster:

    As I've learned from other people here if you set the recordset's cursor location to adUseClient, you can then access the recordcount property. I haven't found it to be any faster or slower than any other method but your mileage may vary.

    HTH

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Recordset Question

    Quote Originally Posted by disruptivehair
    In my experiments I haven't found EOF to be any faster but my ADO code is probably not that great anyway.
    It tends to only be obvious if you are dealing with lots of data, or with certain options (such as ForwardOnly, which makes filling lists etc very quick), but I prefer to 'push' the proper methods anyway, to save future problems.

  5. #5
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Recordset Question

    Quote Originally Posted by si_the_geek
    It tends to only be obvious if you are dealing with lots of data, or with certain options (such as ForwardOnly, which makes filling lists etc very quick), but I prefer to 'push' the proper methods anyway, to save future problems.
    I don't know what the proper methods are...I tend to just go with whatever works the first time. Still learning! I don't even have any books or references on ADO.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Recordset Question

    I don't have any computer related books (except for one about a dodgy printer language I needed for one project), I generally just use the help files and pages at MSDN, along with my experience and common sense (this one is sometimes lacking!).

    If you want us to check/improve your code, feel free to post it in an appropriate forum (perhaps "Code It Better").

  7. #7
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Recordset Question

    Quote Originally Posted by si_the_geek
    I don't have any computer related books (except for one about a dodgy printer language I needed for one project), I generally just use the help files and pages at MSDN, along with my experience and common sense (this one is sometimes lacking!).

    If you want us to check/improve your code, feel free to post it in an appropriate forum (perhaps "Code It Better").

    Good idea; I'd love advice/tips so I can improve. I'll post some today.

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Recordset Question

    This RECORDCOUNT issue comes up so often...

    As SI already said, -1 means there are records, but since they have not all been given to the client from the server, the actual count is not known.

    I always ask "why do you need to know the number of records?".

    Old ACCESS style programming would have probably bound the entire RECORDSET to the client - and in those systems you would move back and forth through a recordset.

    We like to only use FORWARDONLY, READONLY recordsets - and load them entirely into a control (flex grid, drop down, print to the printer object). Then we destroy the RECORDSET and work the data in the client and do updates and such with ACTION QUERIES. With that said we never need to know a recordcount up front - we simply load rows by rows until EOF...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Recordset Question

    Quote Originally Posted by Nervotrep
    Hello!

    VB Code:
    1. rs.Open "SELECT * FROM Customers", cn, adOpenKeyset, adLockOptimistic

    The recordset opens just fine and allows me to .Addnew
    Also, rs.EOF is false

    So why does rs.Recordcount return a -1 when that recordset actually contains records?

    Could it be some permissions problem with my SQL server SA account?

    Thanks in advance!
    You loading the entire table just to add a record? If you already know how to use SELECT queries then its time you also learned to use INSERT, UPDATE and DELETE queries.

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