Results 1 to 6 of 6

Thread: dao in access

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168

    Question

    Hello, I have a question about dao recordsets in MS Access's VBA. Here is my code:

    Code:
    Dim db As DAO.Database
    dim rs as DAO.Recordset
    
    Set db = DBEngine.Workspaces (0) (0)
    set rs = db.OpenRecordset("SELECT * FROM Table1")
    
    Debug.Print rs.RecordCount
    rs.RecordCount ALWAYS returns 1 no matter how many records there are. I've tried to open it in SnapShot, Dynamic, Dynaset but I still get the same problem. I tried to open it in dbOpenTable mode but I get an error and it won't open the recordset. Any solutions?

    Thanks,
    Thai

  2. #2
    Addicted Member
    Join Date
    Oct 1999
    Location
    Brossard, Québec, Canada
    Posts
    241
    .Recordcount works strangely. If you just populated your recordset, you are on the first record and it will return 1. Before looking the .recordcount, you should do .movelast and then .recordcount.

    This should work.

    (strange because I THINK that if you do .movelast, then .movefirst, then .recordcount, it'll give you the good number of records you look for)

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    ...

    Any move (first,last)before the
    debug.print rs.recordset
    should give recordcount


    [Edited by HeSaidJoe on 06-29-2000 at 12:33 PM]
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    I beleieve the rules for DAO recordsets are as follows:
    If you open the recordset as table, as in:
    set rs = db.OpenRecordset("Table1", dbOpenTable)
    Then rs.RecordCount will return the actual number of records in the table.

    But -- if you open the recordset as a dynaset or snapshot, as in:
    set rs = db.OpenRecordset("Select * From Table1", dbOpenDynaset)
    Then rs.RecordCount will return the number of records visited! (And when you first open a dynaset, only one record has been visited, so RecordCount will always be 1.) So as the others mentioned, you must do a MoveLast to force Jet to "look at" or "visit" all the records and return an accurate Recordcount.

    P.S. - If you omit "dbOpenTable" or "dbOpenDynaset", Jet will make certain assumptions: If you give a table name, it will assume "dbOpenTable"; if you give a SQL statement, it will assume "dbOpenDynaset".
    "It's cold gin time again ..."

    Check out my website here.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    Thanks everyone for the help..

    BruceG: When I use an SQL statement and specify dbOpenTable, it gives me an error, why is that?

    Thanks,
    Thai

  6. #6
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    When you open a recordset with "dbOpenTable", Jet expects the first argument to be the name of a single object (i.e. a table in the database). You can open a single table with dbOpenDynaset, but as you've seen, the reverse is not true (you can't use a SQL statement with "dbOpenTable").

    As you may or may not know, there are advantages and disadvantages with each type. (Example: with tables you can set the Index property and use the Seek method for faster searching; with dynasets, you can use SQL to perform joins and you can use the Find methods).
    "It's cold gin time again ..."

    Check out my website here.

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