Solved

How to get a record count using ADO in visualbasic

Posted on 2002-04-18
12
201 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 70

Accepted Solution

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

Expert Comment

by:lschuele
ID: 6951297
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
ID: 6951389
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 4

Expert Comment

by:nutwiss
ID: 6951491
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
ID: 6951559
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
ID: 6951567
>>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
 
LVL 4

Expert Comment

by:nutwiss
ID: 6951621
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
ID: 6951872
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
ID: 6952115
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
ID: 6952569
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
ID: 6952803
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
ID: 6954840
emoreau

I appreciate your response and the answer.

I really appreciate other comments too.

VM
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

815 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

11 Experts available now in Live!

Get 1:1 Help Now