Results 1 to 8 of 8

Thread: Record count in VBA returns -1

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    4

    Unhappy Record count in VBA returns -1

    Hello,

    I am attempting to count the number of records returned by a query in VBA. When my query returns less than 100 records I get a correct record count. Anything over 99 and I get a -1 for my record count. Here is my code:

    'start code
    strConnect = "ODBC;DSN=nip_profile_fabrics_local;UID=;PWD=;Database=fabric_nip_profile.mdb" 'Connection String
    strSQL = "SELECT * FROM [Compression Testing];"

    Set wrkODBC = DBEngine.CreateWorkspace("NewODBCDirect", "", "", dbUseODBC) 'Create ODBCDirect Workspace
    Set conFabric = wrkODBC.OpenConnection("test", dbDriverNoPrompt, True, strConnect) 'Open connection

    Set rstFabric = conFabric.OpenRecordset(strSQL, dbOpenSnapshot) 'Open recordset

    'need to run through the records to get a good count
    rstFabric.MoveLast
    rstFabric.MoveFirst

    ScrollBar1.Min = 1
    ScrollBar1.Max = rstFabric.RecordCount
    ScrollBar1.Value = rstFabric.AbsolutePosition + 1

    'end code

    Help!
    Thanks, John

  2. #2
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Not sure about VBA but the ASP Recordset objects do not report RecordCount correctly either. I just traverse the entire recordset and count the iterations.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    4
    Thanks Dave. I think that is what I may try, unless someone has some other advice.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Since you are using DAO, dbOpenSnapshot does support the
    recordcount property. Just as you describe that it works up to 99.
    If it didn't support the property then you would always get -1 no
    matter how many records are returned. I think you need to
    specify the rest of the parameters for OpenRecordset.

    From MSDN:
    For Connection and Database objects:

    Set recordset = object.OpenRecordset (source, type, options, lockedits)

    You may need to specify the options and lockedits.

    You may also use the SQL Count function to determine the
    number of records returning in the recordset.

    Also, recordcount is not supported if the source of the query is a linked table.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    I should then clarify - I was speaking of ADO Recordsets.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Yes, if it was ADO then you would of been correct.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    I have never had an experience where the .RecordCount property wasn't supported.

    Now.... depending on the cursor location (client vs. server) and the record type (keyset, dynaset, etc) you will get -1 for your recordcount.... typically that's because ADO doesn't know how many records there are - yet. If you get a client side cursor dataset, recordcount will be accurate. If using a server side, and your record type supports it (a forward only one won't be able to do this) you can "jiggle" it to get the recordset by doing a .MoveLast then a .MoveFirst. This will cause ADO to move to the last record, after which the .RecordCount will be filled right.

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

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    jbreen33 - to further expand on that....
    The reason you get an accurate number below 99 may have to do with page caching. The records are small enough that 99 of them can fit on to a page in memory. So when it returns 10 records, they are all there on the same page, and ADO can see there are 10 and returns 10 as the record count. When the size of the records exceed the page size, then ADO doesn't know specificaly how many records there are, unless you tell it to send all pages (by using a client side cursor) or by "flipping" to the last page with a .MoveLast. At that point, it knows how many pages and records there are.

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

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