Record count wrong (1 Viewer)

John thomas

Registered User.
Local time
Today, 15:09
Joined
Sep 4, 2012
Messages
206
Hi
im New to record sets
and have copied this code as a test

Option Explicit

Public Sub LoadArray()
Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("chimneysweep")

With rs
Debug.Print .RecordCount
.Close

End With

Set rs = Nothing
Set db = Nothing


In my immediate windoww it shows 1 record there are in fact 321 what am i doing wrong
I have tried different table still it only returns 1 record
many thanks john
 

jdraw

Super Moderator
Staff member
Local time
Today, 18:09
Joined
Jan 23, 2006
Messages
15,394
You have to do
.Movelast

before you get an accurate recordcount.

Code:
Option Explicit

Public Sub LoadArray()
Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("chimneysweep")

With rs
[COLOR="Blue"].movelast[/COLOR]
Debug.Print .RecordCount
.Close

End With

Set rs = Nothing
Set db = Nothing
 

MarkK

bit cruncher
Local time
Today, 15:09
Joined
Mar 17, 2004
Messages
8,186
A recordset does not necessarily fully populate itself after opening. Common practice to count all the records would be to move the current record pointer to the last record using the recordset's .MoveLast method. Consider the follow code fragment...
Code:
with currentdb.openrecordset("someTable")
   if not .eof then .movelast
   msgbox .recordcount
   .close
end with
hth :)
 

Users who are viewing this thread

Top Bottom