Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

RecordCount method returning -1 for a recordset

 
   Database Help (Home) -> Visual Basic -> ODBC RSS
Next:  Dynamic report problem  
Author Message
Utf-8BUGF1bCBXaWxs

External


Since: Apr 06, 2004
Posts: 2



(Msg. 1) Posted: Wed Mar 31, 2004 5:21 pm
Post subject: RecordCount method returning -1 for a recordset
Archived from groups: microsoft>public>vb>database>odbc (more info?)

I'm developing an internal database app using Visual Basic. I have a recordset which has data in it - I know this because I am displaying it in my form and am stepping through different records. I want to get the number of records in the recordset and have attempted to do this by using the .RecordCount method of the recordset. This always returns -1. Any idea why this is? Following is the declaration of my odbc connection and the creation of my recordset:

'Global definition for the database
Global DataOracleCon As New ADODB.Connection

'Define SQL string
strNarrHeaderSQL = "SELECT * FROM AR_NARR_DATA"

'Connect via odbc to the database
Set DataOracleCon = New ADODB.Connection
DataOracleCon.ConnectionString = "DSN=GRANTARC;UID=GRMNPROG;PWD=password"
DataOracleCon.Open

'Create narrative recordsets based upon the user's selection
Set rstNarrHeader = New ADODB.Recordset
rstNarrHeader.CursorType = adOpenDynamic 'set cursor to full forward/backward capability
rstNarrHeader.Open strNarrHeaderSQL, DataOracleCon, adOpenDynamic, adLockReadOnly

tempVar = rstNarrHeader.RecordCount 'This always equals -1

Any help or insights on this would be most appreciated.

Thanks,

Paul

 >> Stay informed about: RecordCount method returning -1 for a recordset 
Back to top
Login to vote
Kevin Yu MSFT

External


Since: Nov 20, 2003
Posts: 67



(Msg. 2) Posted: Thu Apr 01, 2004 9:21 am
Post subject: RE: RecordCount method returning -1 for a recordset [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi Paul,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you always get -1 as the returning
value when checking the RecordSet.RecordCount property. If there is any
misunderstanding, please feel free to let me know.

Based on the code you have provided, we can see that currently you are
using a server side cursor. The RecordCount property returns -1 when ADO
cannot determine the number of records or if the provider or cursor type
does not support RecordCount. As the cursor is on server side, ADO does not
know the exact number of records, so it returns -1. As the cursors are
opened at server side by default, to resolved this, use a client side
cursor by adding the following line BEFORE the RecordSet opens.

rstNarrHeader.CursorLocation = adUseClient

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

 >> Stay informed about: RecordCount method returning -1 for a recordset 
Back to top
Login to vote
Utf-8BUGF1bCBXaWxs1

External


Since: Apr 01, 2004
Posts: 1



(Msg. 3) Posted: Thu Apr 01, 2004 5:06 pm
Post subject: RE: RecordCount method returning -1 for a recordset [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks Kevin - that was indeed the problem. Totally unaware that server-side cursors didn't support record counts.

Chalk yet another one up to experience and help from outside sources!

Paul
 >> Stay informed about: RecordCount method returning -1 for a recordset 
Back to top
Login to vote
Kevin Yu MSFT

External


Since: Nov 20, 2003
Posts: 67



(Msg. 4) Posted: Fri Apr 02, 2004 4:12 am
Post subject: RE: RecordCount method returning -1 for a recordset [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi Paul,

It was nice to hear that you have had the problem resolved. Thanks for
sharing your experience with all the people here. If you have any
questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 >> Stay informed about: RecordCount method returning -1 for a recordset 
Back to top
Login to vote
Darwin Weyh

External


Since: Apr 30, 2004
Posts: 3



(Msg. 5) Posted: Mon Apr 05, 2004 10:26 am
Post subject: Re: RecordCount method returning -1 for a recordset [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

So if I understand all this right to get the proper record count I need to
open the recordset twice:
Once with the "select count(*) as reccount where ...."
save the record count and then again with "select * where ...."
to use the records.

Or is there a better way?

Darwin C. Weyh
shtdev.delete.this DeleteThis @weyh.net
And for the spammers: dweyh DeleteThis @weyh.net

"Kevin Yu [MSFT]" <v-kevy DeleteThis @online.microsoft.com> wrote in message
news:Dl8Qc%23EGEHA.2224@cpmsftngxa06.phx.gbl...
 > Hi Paul,
 >
 > It was nice to hear that you have had the problem resolved. Thanks for
 > sharing your experience with all the people here. If you have any
 > questions, please feel free to post them in the community.
 >
 > Kevin Yu
 > =======
 > "This posting is provided "AS IS" with no warranties, and confers no
 > rights."
 ><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: RecordCount method returning -1 for a recordset 
Back to top
Login to vote
Douglas J. Steele

External


Since: Mar 14, 2004
Posts: 1626



(Msg. 6) Posted: Fri Apr 09, 2004 1:41 pm
Post subject: Re: RecordCount method returning -1 for a recordset [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Depending on the type of recordset, you can use MoveLast to go to the end of
the recordset, at which point its RecordCount property will be correct. Once
you've done that, use MoveFirst to go back to the beginning, and do your
processing.

And while a picky point, the example you gave doesn't open the recordset
twice. They're two different recordsets.

--
Doug Steele, Microsoft Access MVP
<a style='text-decoration: underline;' href="http://I.Am/DougSteele" target="_blank">http://I.Am/DougSteele</a>
(No private e-mails, please)



"Darwin Weyh" <shtdev.delete.this.TakeThisOut@weyh.net> wrote in message
news:uD545kwGEHA.1968@TK2MSFTNGP12.phx.gbl...
 > So if I understand all this right to get the proper record count I need to
 > open the recordset twice:
 > Once with the "select count(*) as reccount where ...."
 > save the record count and then again with "select * where ...."
 > to use the records.
 >
 > Or is there a better way?
 >
 > Darwin C. Weyh
 > shtdev.delete.this.TakeThisOut@weyh.net
 > And for the spammers: dweyh.TakeThisOut@weyh.net
 >
 > "Kevin Yu [MSFT]" <v-kevy.TakeThisOut@online.microsoft.com> wrote in message
 > news:Dl8Qc%23EGEHA.2224@cpmsftngxa06.phx.gbl...
  > > Hi Paul,
  > >
  > > It was nice to hear that you have had the problem resolved. Thanks for
  > > sharing your experience with all the people here. If you have any
  > > questions, please feel free to post them in the community.
  > >
  > > Kevin Yu
  > > =======
  > > "This posting is provided "AS IS" with no warranties, and confers no
  > > rights."
  > >
 >
 ><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: RecordCount method returning -1 for a recordset 
Back to top
Login to vote
Display posts from previous:   
   Database Help (Home) -> Visual Basic -> ODBC All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]