Solved

How to get a record count using ADO in visualbasic

Posted on 2002-04-18
12
197 Views
Last Modified: 2010-05-02
I got a select statement like this:
dim sql as string
sql = select * from table1
rs.open sql

If Not RS.EOF Then
        RS.MoveFirst
        rscount = RS.RecordCount
  Else
        rscount = 0
    End If

I have records in table but i get rscount = -1
i got 44 records, how to get number of records count

vm

0
Comment
Question by:vmandem
12 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 35 total points
Comment Utility
set the cursortype property to adOpenStatic
0
 

Expert Comment

by:lschuele
Comment Utility
The bad news is that whether RecordCount returns a value depends on the provider and type of cursor that you are using.  The default cursor type is forward only which does not support RecordCount.

Try changing your open to

rs.Open sql, , adOpenStatic

Static cursors return RecordCount for the SQL Server OLEDB provider.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Actually I beg to differ the RecordCount property depends on the CursorLocation not the CursorType.  The CursorLocation should be set to adUseClient.

What is happeing is that for some providers setting the CursorType to adOpenStatic will implicitly set the CursorLocation to adUseClient.  But this is Provider dependant.

As a side note, you probably should not be using the RecordCount property in the first place.

Anthony
0
 
LVL 4

Expert Comment

by:nutwiss
Comment Utility
Anthony - actually you're both half right...

The ability of the recordset to return its recordcount depends on a number of factors including the provider and cursor location, but however you're opening your recordset, check .Supports(adApproxPosition) or .Supports(adBookmark) returns true, if so then the recordcount property will return the exact number of records in the Recordset.

Recordcount will always return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and -1 or the actual count for a dynamic cursor.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
if all you want is a count of how many records meet your requirement then why not simply return the count in the Query?

dim sql as string
dim rsCount as Integer

sql = "select count(*) as Recordcount from table1"
rs.open sql

rsCount = rs("RecordCount")

then you can proceed , if the count is >0:

if RsCount > 0 then
   sql = "Select * from Table1"

   rs.open sql
else
   MsgBox "There Are no records to retrieve", VBOKOnly
end if


0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>Anthony - actually you're both half right...<<
I guess you must be talking about my left half and my right half <g>

>> .Supports(adApproxPosition) or .Supports(adBookmark) returns true<<
The first is correct the second is not (see the next point)

>> Recordcount will always return -1 for a forward-only cursor<<
This is not true, try this code and you will see what I mean:

Dim rs As ADODB.Recordset

Set rs = New ADODB.Recordset
With rs
    .Source = "select * from table1"
    .ActiveConnection = "Provider=SQLOLEDB.1; ..."
    .CursorType = adOpenForwardOnly
    .CursorLocation = adUseClient
    .LockType = adLockReadOnly
    .Open Options:=adCmdText
    Debug.Print .RecordCount
    .Close
End With
Set rs = Nothing

Again, the use of the RecordCount property should not be encouraged.  Not least of its evils, is that it is provider dependant.

Having said that the facts speak for themselves.

Anthony
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 4

Expert Comment

by:nutwiss
Comment Utility
fair enough - I was only quoting MSDN - but it wouldn't be the first time MS were wrong, would it?

:)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Arthur_Wood has given the correct answer (among several that I can think of) as to how to calculate the record count.  This is totally Provider/CursorType/ CursorLocation independent.

Anthony
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
Why do you need "rscount"?
Q: What do you think, which is faster: Getting the record count or retrieving all records?

I use GetRows to get data into variant array. To show record count, I use:
     numitems = UBound(avarData, 2) + 1
0
 

Expert Comment

by:talluri_28
Comment Utility
hi

before rs.open "select Statement"

u have to write   rs.cursorlocation = aduseclient
then u will get
ex

 if rs.state = adstateopen then rs.close

 rs.cursorlocation = aduseclient
 rs.open "select Statement",connection
 rs.recordcount

jag



0
 
LVL 2

Expert Comment

by:corvanderlinden
Comment Utility
If Not RS.EOF and RS.BOF Then
       RS.MoveLast
       RS.MoveFirst
       rscount = RS.RecordCount
 Else
       rscount = 0
   End If

Or change your cursor type as suggested above
0
 

Author Comment

by:vmandem
Comment Utility
emoreau

I appreciate your response and the answer.

I really appreciate other comments too.

VM
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now