vba 为什么此记录计数返回 1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15321849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:56:38  来源:igfitidea点击:

Why is this record count returning 1?

vbams-accessaccess-vba

提问by Katana24

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:

在我的 MS Access DB 中,我在 VB 中运行一个应该返回两条记录的查询。当我在 SQL 中运行它时,我得到两条记录,但是当从 VBA 运行时,我得到两条记录。这是 SQL 视图中获取两条记录的代码:

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

And when I call this in VBA like so:

当我在 VBA 中这样调用它时:

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?

我得到一个记录数的记录。记录计数不是假设计算从 SQL 语句或表返回的所有记录吗?我在这里做错了什么?

Thanks

谢谢

回答by grahamj42

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.MoveLastafter opening the recordset and before retrieving rst.RecordCOunt.

除了最简单的查询(性能优化)之外,DAO 不会一次检索整个结果集。要rst.MoveLast在打开记录集之后和检索之前强制执行完整检索和有效的记录计数,请使用 rst.RecordCOunt

回答by mohamed mostafa

for recordcount property you need to set cursor type as

对于 recordcount 属性,您需要将游标类型设置为

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

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

如果它不能执行那么你需要使用 ADOVBS.INC 作为你页面顶部的包含文件,你可以从互联网上下载和使用它