"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