vba DAO.Recordset在codese中执行时只返回一条记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8882739/
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 14:56:57  来源:igfitidea点击:

DAO.Recordset returns only one record when executing in codese

vbams-access-2007dao

提问by Smith

This statement always returns only one record everytime when its executed

该语句每次执行时总是只返回一条记录

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Select book_name from book")
Dim i As Integer
For i = 0 To rs.RecordCount - 1
lbBooks.AddItem rs!book_name
rs.MoveNext
Next

what could ebe the cause

可能是什么原因

回答by Xophmeister

I believe that the RecordCountproperty of a Recordsetis set dynamically to the amount of data that has been read from the cursor. That is, when it's first opened, it's set to 1; if you do rs.MoveLast, it will set it to the actual number of records in the set. However, you then have the problem of moving back to the start: you must have the recordset opened in a particular mode (which I forgot, from the top of my head) to be able to arbitrarily move the cursor pointer back and forward.

我相信RecordCounta的属性Recordset是动态设置为从游标读取的数据量。即第一次打开时,设置为1;如果这样做rs.MoveLast,它会将其设置为集合中的实际记录数。但是,您随后会遇到返回起点的问题:您必须以特定模式打开记录集(我从头顶上忘记了),以便能够任意前后移动光标指针。

The usual way of iterating through a cursor is to use a whileloop, checking for the cursor's end of file:

遍历游标的常用方法是使用while循环,检查游标的文件末尾:

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Select book_name from book")
Dim i As Integer

i = 0 
While Not rs.EOF
  lbBooks.AddItem rs!book_name
  rs.MoveNext
  i = i + 1
Wend