Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this record count returning 1?

Tags:

vba

ms-access

In my MS Access DB I'm running a query in VB that should return two records. When I run it in SQL I get two records but when ran from VBA I get two. Here is the code in the SQL view which gets two records:

SELECT *
FROM tblWebMeetingData
WHERE [Last Name] LIKE 'Marx';

And when I call this in VBA like so:

SQL = "SELECT * FROM tblWebMeetingData WHERE [Last Name] LIKE 'Marx';"
Set rst = CurrentDb.OpenRecordset(SQL)  
MsgBox ("Number of records: " & rst.RecordCount)

I get one record for number of records. Isn't record count suppose to count all the records returned from a SQL statement or table? What is it I'm doing wrong here?

Thanks

like image 441
Katana24 Avatar asked Mar 10 '13 11:03

Katana24


2 Answers

DAO doesn't retrieve the entire result set at once for all but the simplest queries (performance optimisation). To force a complete retrieval and a valid recordcount use rst.MoveLast after opening the recordset and before retrieving rst.RecordCOunt.

like image 129
grahamj42 Avatar answered Oct 14 '22 07:10

grahamj42


for recordcount property you need to set cursor type as

RS.Open SQL, MyConn, adOpenStatic, adLockReadOnly, adCmdText

if it can not execute then you need to use ADOVBS.INC as include file in the top of your page you can download and use it from the internet

like image 37
mohamed mostafa Avatar answered Oct 14 '22 07:10

mohamed mostafa