Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Recordset Recordcount

Recordset Recordcount

Scheduled Pinned Locked Moved Database
help
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MikeMarq
    wrote on last edited by
    #1

    I'm trying to find out how many records are in a table using this code but I keep getting -1 for n. Dim command As String, data As ADODB.Recordset, n As Integer rsGuest.Open("Guest", ADOConnection,ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic) command = "SELECT [Guest ID] from Guest" data = ADOConnection.Execute(command) n = data.RecordCount I read on another webisite that I need to add this line: data.CursorLocation = ADODB.CursorLocationEnum.adUseClient but when I do I get this error: Operation is not allowed when the object is open. So what should I do. Thank you for your help. Mike

    M L B 3 Replies Last reply
    0
    • M MikeMarq

      I'm trying to find out how many records are in a table using this code but I keep getting -1 for n. Dim command As String, data As ADODB.Recordset, n As Integer rsGuest.Open("Guest", ADOConnection,ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic) command = "SELECT [Guest ID] from Guest" data = ADOConnection.Execute(command) n = data.RecordCount I read on another webisite that I need to add this line: data.CursorLocation = ADODB.CursorLocationEnum.adUseClient but when I do I get this error: Operation is not allowed when the object is open. So what should I do. Thank you for your help. Mike

      M Offline
      M Offline
      MikeMarq
      wrote on last edited by
      #2

      forgot to add I'm doing this with vb.net 2005 with microsoft access 2007.

      R 1 Reply Last reply
      0
      • M MikeMarq

        I'm trying to find out how many records are in a table using this code but I keep getting -1 for n. Dim command As String, data As ADODB.Recordset, n As Integer rsGuest.Open("Guest", ADOConnection,ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic) command = "SELECT [Guest ID] from Guest" data = ADOConnection.Execute(command) n = data.RecordCount I read on another webisite that I need to add this line: data.CursorLocation = ADODB.CursorLocationEnum.adUseClient but when I do I get this error: Operation is not allowed when the object is open. So what should I do. Thank you for your help. Mike

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, you could try a SELECT COUNT(*) FROM Guest and use ExecuteScalar, then cast to integer. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


        M 1 Reply Last reply
        0
        • M MikeMarq

          I'm trying to find out how many records are in a table using this code but I keep getting -1 for n. Dim command As String, data As ADODB.Recordset, n As Integer rsGuest.Open("Guest", ADOConnection,ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic) command = "SELECT [Guest ID] from Guest" data = ADOConnection.Execute(command) n = data.RecordCount I read on another webisite that I need to add this line: data.CursorLocation = ADODB.CursorLocationEnum.adUseClient but when I do I get this error: Operation is not allowed when the object is open. So what should I do. Thank you for your help. Mike

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          If you want to get maximum value of Guest ID then use this query select max([Guest ID]) from Guest If you have to get number of rows on table then refer to Luc Pattyn's answer.


          I Love T-SQL

          1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, you could try a SELECT COUNT(*) FROM Guest and use ExecuteScalar, then cast to integer. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


            M Offline
            M Offline
            MikeMarq
            wrote on last edited by
            #5

            Hi thank you both for your help. I don't have much experience with database programming and I'm not sure what ExecuteScalar is a method of or how I would use it. To clarify the question of whether I was trying to get the number of rows or the highest id number, I was looking for the number of rows. thanks again Luc and blueboy, Mike

            B 1 Reply Last reply
            0
            • M MikeMarq

              Hi thank you both for your help. I don't have much experience with database programming and I'm not sure what ExecuteScalar is a method of or how I would use it. To clarify the question of whether I was trying to get the number of rows or the highest id number, I was looking for the number of rows. thanks again Luc and blueboy, Mike

              B Offline
              B Offline
              Blue_Boy
              wrote on last edited by
              #6

              Check this link[^], you can learn more about ExcuteScalar method.


              I Love T-SQL

              1 Reply Last reply
              0
              • M MikeMarq

                forgot to add I'm doing this with vb.net 2005 with microsoft access 2007.

                R Offline
                R Offline
                Rob Graham
                wrote on last edited by
                #7

                If you are using vb.net , I would recomment using the System.Data.Oledb classes rather than com interop with ADODB. It would be both easier and more performant. To get the number of records currently in a table Use "Select Count(*) as nRecs from Mytable" for your query. For your ADODB approach to work, you have to navigate to the end of the recordset before the count is accurate (use Recordset.MoveLast). Obviously, this is slow... To changed to a client side cursor, set the cursortype variable BEFORE opening the connection. Hope thies suggestions help.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups