record count problem (1 Viewer)

lana

Registered User.
Local time
Tomorrow, 01:45
Joined
Feb 10, 2010
Messages
92
Hi there,
I have a table which has 4 records. I use the followings to count the records:
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("tbl_calc")
rst.MoveLast
aaa = rst.RecordCount
rst.MoveFirst

this returns 44 instead of 4 !!!!!
Any ideas?
Thanks
 

isladogs

MVP / VIP
Local time
Today, 22:15
Joined
Jan 14, 2017
Messages
18,209
You're seeing double? Too much alcohol?

Try this:

Code:
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("tbl_calc")
rst.MoveFirst
rst.MoveLast
aaa = rst.RecordCount
 

MarkK

bit cruncher
Local time
Today, 14:15
Joined
Mar 17, 2004
Messages
8,180
both will fail if the recordset it empty, try...
Code:
with currentdb.openrecordset(sql)
   if not .eof then .movelast
   GetCount = .recordcount
   .close
end with
To get a RecordCount you want to check for EOF, and you never need to MoveFirst.
 

isladogs

MVP / VIP
Local time
Today, 22:15
Joined
Jan 14, 2017
Messages
18,209
Hi

Well actually I meant to write

Code:
Code:
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("tbl_calc")
rst.MoveLast
rst.MoveFirst
aaa = rst.RecordCount

That's how I normally do it
However, having read the last post from MarkK, I've checked and he is indeed right.
I've been doing an unnecessary MoveFirst line for years!
 

static

Registered User.
Local time
Today, 22:15
Joined
Nov 2, 2015
Messages
823
aaa = currentdb.OpenRecordset("select count(1) from tbl_calc")(0)
 

lana

Registered User.
Local time
Tomorrow, 01:45
Joined
Feb 10, 2010
Messages
92
Thank you all.

Actually my own code works. I compacted the database then it worked?!

Table in question is a temp one and the data in it is cleared all the time.
Maybe something happens when data is deleted.

Thanks a lot.
 

Users who are viewing this thread

Top Bottom