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

Recordcount usage

 
   Database Forums (Home) -> Visual Basic -> DAO RSS
Next:  How to query a database and compare value entered..  
Author Message
Damian

External


Since: Mar 01, 2007
Posts: 7



(Msg. 1) Posted: Thu Mar 01, 2007 10:39 am
Post subject: Recordcount usage
Archived from groups: microsoft>public>vb>database>dao (more info?)

Hello all,

I have code that in some cases works with recordCount and others doesnt
(always returns -1 even if there are records).

Is there a rule about using recordcount or alternative other than looping
through a recordet and counting manually?

Thanks

 >> Stay informed about: Recordcount usage 
Back to top
Login to vote
Jeff

External


Since: Mar 01, 2007
Posts: 1



(Msg. 2) Posted: Thu Mar 01, 2007 10:39 am
Post subject: Re: Recordcount usage [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You need to do the following:

rs.movelast
rs.movefirst

The recordcount will then have a correct value.

Just make sure the recordset is not forward only!



"Damian" wrote in message

> Hello all,
>
> I have code that in some cases works with recordCount and others doesnt
> (always returns -1 even if there are records).
>
> Is there a rule about using recordcount or alternative other than looping
> through a recordet and counting manually?
>
> Thanks
>

 >> Stay informed about: Recordcount usage 
Back to top
Login to vote
Norm Cook1

External


Since: Jun 27, 2004
Posts: 24



(Msg. 3) Posted: Thu Mar 01, 2007 10:39 am
Post subject: Re: Recordcount usage [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Damian" wrote in message

> Hello all,
>
> I have code that in some cases works with recordCount and others doesnt
> (always returns -1 even if there are records).
>
> Is there a rule about using recordcount or alternative other than looping
> through a recordet and counting manually?
>
> Thanks

In general, if a query returns a recordset that contains any records
it will have a RecordCount = 1 until you do a MoveLast. If it does
not contain any records, then the RecordCount property will
return a runtime error. Here's a function which may help.
If it returns True then RecordCount will be available, otherwise
the query failed.

Private Function HasRecords(RS As RecordSet) As Boolean
If Not (RS.EOF And RS.BOF) Then
HasRecords = True
RS.MoveLast
RS.MoveFirst
End if
End Function
 >> Stay informed about: Recordcount usage 
Back to top
Login to vote
Damian

External


Since: Mar 01, 2007
Posts: 7



(Msg. 4) Posted: Thu Mar 01, 2007 3:47 pm
Post subject: Re: Recordcount usage [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Norm Cook" wrote in message

> "Damian" wrote in message
>
>> Hello all,
>>
>> I have code that in some cases works with recordCount and others doesnt
>> (always returns -1 even if there are records).
>>
>> Is there a rule about using recordcount or alternative other than looping
>> through a recordet and counting manually?
>>
>> Thanks
>
> In general, if a query returns a recordset that contains any records
> it will have a RecordCount = 1 until you do a MoveLast. If it does
> not contain any records, then the RecordCount property will
> return a runtime error. Here's a function which may help.
> If it returns True then RecordCount will be available, otherwise
> the query failed.
>
> Private Function HasRecords(RS As RecordSet) As Boolean
> If Not (RS.EOF And RS.BOF) Then
> HasRecords = True
> RS.MoveLast
> RS.MoveFirst
> End if
> End Function
>
>

OK Thanks - will give that a go. Although I think I already did this and it
still didnt give me the right recordcount Neutral... will try the above once more
and let you know if I have any problems...
 >> Stay informed about: Recordcount usage 
Back to top
Login to vote
Damian

External


Since: Mar 01, 2007
Posts: 7



(Msg. 5) Posted: Thu Mar 01, 2007 4:00 pm
Post subject: Re: Recordcount usage [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Norm Cook" wrote in message

> "Damian" wrote in message
>
>> Hello all,
>>
>> I have code that in some cases works with recordCount and others doesnt
>> (always returns -1 even if there are records).
>>
>> Is there a rule about using recordcount or alternative other than looping
>> through a recordet and counting manually?
>>
>> Thanks
>
> In general, if a query returns a recordset that contains any records
> it will have a RecordCount = 1 until you do a MoveLast. If it does
> not contain any records, then the RecordCount property will
> return a runtime error. Here's a function which may help.
> If it returns True then RecordCount will be available, otherwise
> the query failed.
>
> Private Function HasRecords(RS As RecordSet) As Boolean
> If Not (RS.EOF And RS.BOF) Then
> HasRecords = True
> RS.MoveLast
> RS.MoveFirst
> End if
> End Function
>
>

I was using RS.Movelast but nor adding RS.Movefirst afterwards and it still
worked in some places. Is there a potential problem with that? Have now
added the RS.MoveFirs to all the statments just in case!
 >> Stay informed about: Recordcount usage 
Back to top
Login to vote
Henning

External


Since: Jan 15, 2006
Posts: 20



(Msg. 6) Posted: Fri Mar 02, 2007 2:07 am
Post subject: Re: Recordcount usage [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Damian" skrev i meddelandet

> Hello all,
>
> I have code that in some cases works with recordCount and others doesnt
> (always returns -1 even if there are records).
>
> Is there a rule about using recordcount or alternative other than looping
> through a recordet and counting manually?
>
> Thanks
>
>
..recordcount only works with certain drivers, cursors etc.. If 'wrong' it
returns -1.
Try using dbOpenDynamic and don't set any cursor, ie use default cursor.

/Henning
 >> Stay informed about: Recordcount usage 
Back to top
Login to vote
James

External


Since: Feb 07, 2009
Posts: 1



(Msg. 7) Posted: Sat Feb 07, 2009 10:52 am
Post subject: Re: Recordcount usage [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Damian" wrote in message

> Hello all,
>
> I have code that in some cases works with recordCount and others doesnt
> (always returns -1 even if there are records).
>
> Is there a rule about using recordcount or alternative other than looping
> through a recordet and counting manually?
>
> Thanks
Have you ever resolved this RecordCount question?

I have the same problem after opening a recordset. Have you seen any
working examples?
 >> Stay informed about: Recordcount usage 
Back to top
Login to vote
Ralph

External


Since: Jun 25, 2005
Posts: 348



(Msg. 8) Posted: Sat Feb 07, 2009 1:22 pm
Post subject: Re: Recordcount usage [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"James" wrote in message

>
> "Damian" wrote in message
>
> > Hello all,
> >
> > I have code that in some cases works with recordCount and others doesnt
> > (always returns -1 even if there are records).
> >
> > Is there a rule about using recordcount or alternative other than
looping
> > through a recordet and counting manually?
> >
> > Thanks
> Have you ever resolved this RecordCount question?
>
> I have the same problem after opening a recordset. Have you seen any
> working examples?
>

Are you asking about DAO or ADO?

With DAO it is difficult to provide a "hard rule" on the validity of the
RecordCount property as it is dependent on the options used in opening the
recordset and environment in which DAO is being used. For example, a direct
query on a local database may give different results compared to a query
performed by a VB app against a linked-table in MSAccess.

But a General Rule is the Recordcount is the number of records that have
been "READ" from the recordset.
"How to: Count the Number of Records in a DAO Recordset"
http://msdn.microsoft.com/en-us/library/bb243791.aspx

A simpler solution is to use COUNT() in your query ...
Set rs = db.OpenRecordSet("SELECT colA, colB, Count(*) As cnt FROM
tableJunk, <options>) ' air code
Debug.Print rs!cnt

With ADO, whether or not the RecordCount is valid will depend entirely on
the cursor type you used. "-1" indicates "RecordCount is not supported for
this query".

You shouldn't use a RecordCount to just 'step' through a query. Simply
'loop' through the recordset using While NOT rs.EOF, etc. If you need it
then specifically query for it, or count as you step through the records.

hth
-ralph
 >> Stay informed about: Recordcount usage 
Back to top
Login to vote
Norm Cook

External


Since: Aug 27, 2003
Posts: 4



(Msg. 9) Posted: Sun Feb 08, 2009 7:20 am
Post subject: Re: Recordcount usage [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Ralph" wrote in message

>
> "James" wrote in message
>
>>
>> "Damian" wrote in message
>>
>> > Hello all,
>> >
>> > I have code that in some cases works with recordCount and others doesnt
>> > (always returns -1 even if there are records).
>> >
>> > Is there a rule about using recordcount or alternative other than
> looping
>> > through a recordet and counting manually?
>> >
>> > Thanks
>> Have you ever resolved this RecordCount question?
>>
>> I have the same problem after opening a recordset. Have you seen any
>> working examples?
>>
>
> Are you asking about DAO or ADO?
>
> With DAO it is difficult to provide a "hard rule" on the validity of the
> RecordCount property as it is dependent on the options used in opening the
> recordset and environment in which DAO is being used. For example, a
> direct
> query on a local database may give different results compared to a query
> performed by a VB app against a linked-table in MSAccess.
>
> But a General Rule is the Recordcount is the number of records that have
> been "READ" from the recordset.
> "How to: Count the Number of Records in a DAO Recordset"
> http://msdn.microsoft.com/en-us/library/bb243791.aspx
>
> A simpler solution is to use COUNT() in your query ...
> Set rs = db.OpenRecordSet("SELECT colA, colB, Count(*) As cnt FROM
> tableJunk, <options>) ' air code
> Debug.Print rs!cnt
>
> With ADO, whether or not the RecordCount is valid will depend entirely on
> the cursor type you used. "-1" indicates "RecordCount is not supported for
> this query".
>
> You shouldn't use a RecordCount to just 'step' through a query. Simply
> 'loop' through the recordset using While NOT rs.EOF, etc. If you need it
> then specifically query for it, or count as you step through the records.
>
> hth
> -ralph

Ralph, your Count(*) sql above produces an error unless you include the
'Group By' clause and then rs!cnt returns the value 1.

IMV the most reliable method to return a true recordcount is
If not rs.eof then
rs.movelast
rs.movefirst
end if

Then you can use the construct
for i = 1 to rs.recordcount
...do whatever
rs.movenext
next
which is much faster than
do while not rs.eof etc.
 >> Stay informed about: Recordcount usage 
Back to top
Login to vote
Ralph

External


Since: Jun 25, 2005
Posts: 348



(Msg. 10) Posted: Sun Feb 08, 2009 8:57 am
Post subject: Re: Recordcount usage [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Norm Cook" wrote in message

<snipped>
> >
> > A simpler solution is to use COUNT() in your query ...
> > Set rs = db.OpenRecordSet("SELECT colA, colB, Count(*) As cnt FROM
> > tableJunk, <options>) ' air code
> > Debug.Print rs!cnt
> >
<snipped>
>
> Ralph, your Count(*) sql above produces an error unless you include the
> 'Group By' clause and then rs!cnt returns the value 1.
>
> IMV the most reliable method to return a true recordcount is
> If not rs.eof then
> rs.movelast
> rs.movefirst
> end if
>
> Then you can use the construct
> for i = 1 to rs.recordcount
> ...do whatever
> rs.movenext
> next
> which is much faster than
> do while not rs.eof etc.
>

You're absolutely correct, and thanks for catching that.
In my haste, I confused ADO/DAO and was thinking along the lines of using
MSAccess's "DBCount" or a TableDef.Recordcount, ... (My excuse is the
lateness of the hour. <g>)

The "General Rule" then becomes "the Recordcount is the number of records
that have been "READ" from the recordset. So a total count is only reliable
after all records have been "touched".

-ralph
 >> Stay informed about: Recordcount usage 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
strange! vb6+access2002 - I have a mdb called treesy.mdb.There is a table,named transcipt with three fields:serial_no, eng,sound serial_no:1-10 (10 numbers) eng: 1.The movie has been on for 15 minutes, madam.@ 2.Oh, my god!@ It's 7:45 now!@ 3.Is Helen coming?@ It¡¯s 6:50 now.@..

Authentication Error??? - Ok so heres the code.. for some reason I kept getting the no permissions message, so I attempted to add the access workgroup file. Now I get the "Authentication Failed" message. This is a secure database. HELP.. I know I'm writing (adding) som...

Database Access Crashes on Windows 2003 Terminal Server - I have been running a Microsoft Access 97 application on Windows 2003 Terminal Server for almost a year now, with no problems. About two weeks ago, I started to get the following errors in my event log: Application Error; EventID: 1000 Faulting..

Software Engineers required for USA - Dear Sir, Kraftware Inc. Software Engineers required for USA We are a leading software consulting company based in Nebraska (Omaha) Growing in business for the past 9 yrs. We are currently Experiencing Growth and we are Hiring Software Engineers for..

Clipboard content to Access field - Hello: How can I paste a bitmap image in clipboard to an Access field using VB? Thank you in advance.
   Database Forums (Home) -> Visual Basic -> DAO All times are: Pacific Time (US & Canada)
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 ]