MS Access VBA .recordcount 在记录存在时返回 0,并且 debug.print returns 值
MS Access VBA .recordcount returning 0 when records exist, and debug.print returns value
我的 Access table 有 10 条记录和一个短文本字段。我正在使用 .recordcount 函数 return 此 table 中的记录数。代码如下:
Dim db as DAO.Database
Dim RS as DAO.Recordset
Dim recCount as Integer
Set db = CurrentDb
Set RS = db.OpenRecordset("Table Name")
RS.MoveFirst
RS.MoveLast
recCount = RS.recordcount
Debug.Print(recCount)
Dim i as Integer
i = 0
RS.MoveFirst
'Option one. Commented out when option two is active and vice verse
Do While i < 10
Debug.Print(RS(i))
i = i + 1
Loop
Do While i < 10
Debug.print(RS![Only Field Name])
i = i + 1
RS.MoveNext
Loop
recCount 始终打印为 0。尝试打印记录集中的记录将 return 仅是记录集中的第一个值,没有其他。读取第一条记录后,程序抛出错误 "Item not found in collection." 我不确定是什么导致了这个错误,因为我在另一个 VBA 模块中使用了与另一个 table 完全相同的方法,效果很好。
我在别处查看解决方案,我唯一能找到的是在打开后添加 RS.moveFirst
和 RS.moveLast
,但这不起作用。我认为这是因为打开的记录集实际上并不包含 table.
中的所有记录
提前致谢。
编辑:
试试这个:
Sub Demo_IterateRecords()
Const tblName = "YOUR TABLE NAME HERE"
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(tblName)
With rs
.MoveLast
.MoveFirst
If MsgBox("Do you want to list all " & .RecordCount & " records?", _
vbOKCancel, "Confirmation") <> vbOK Then GoTo ExitMySub
Do While Not .EOF
Debug.Print .Fields(0), .Fields(1), .Fields(2)
rs.MoveNext
Loop
ExitMySub:
.Close
End With
Set rs = Nothing
End Sub
我使用 .Fields(_)
是因为我不确定您的字段叫什么,但更好的引用方式是按名称,例如:
Debug.Print !myID, !myEmployeeName, !myEmployeeAddress
原答案:
试试这个:
RS.MoveLast
RS.MoveFirst
recCount = RS.RecordCount
Debug.Print(recCount)
在您至少浏览一次记录之前,Access 不知道有多少条记录。
如果您在 循环后检查 RS.RecordCount
的值,您会得到一个数字。
Remarks
Use the Recordcount
property to find out how many records in a Recordset or TableDef
object have been accessed. The RecordCount
property doesn't indicate how many records are contained in a dynaset–, snapshot–, or forward–only–type Recordset object until all records have been accessed. Once the last record has been accessed, the RecordCount
property indicates the total number of undeleted records in the Recordset or TableDef object. To force the last record to be accessed, use the MoveLast method on the Recordset object. You can also use an SQL Count
function to determine the approximate number of records your query will return.
Important Note
Using the MoveLast
method to populate a newly opened Recordset negatively impacts performance. Unless it is necessary to have an accurate RecordCount
as soon as you open a Recordset, it's better to wait until you populate the Recordset with other portions of code before checking the RecordCount
property.
(Source)
另请参阅:MSDN:Recordset.RecordCount Property
我设法解决了这个问题,但我不知道为什么会这样。我没有创建新的 table 并输入十条记录的值,而是使用插入查询将我想要的查询值放入 table。使用这个新的 table,它成功了。
您可以边数边列出记录:
Set RS = db.OpenRecordset("Table Name")
While Not RS.EOF
Debug.Print RS![Only Field Name].Value
i = i + 1
RS.MoveNext
Loop
Debug.Print i & " records found."
也许你可以在你的子例程中尝试类似的东西:
Dim db As DAO.Database
Dim RS As DAO.Recordset
Dim recCount As Integer
Set db = CurrentDb
Set RS = db.OpenRecordset("Table Name")
If Not (RS.EOF And RS.BOF) Then
RS.MoveFirst
Do Until RS.EOF = True
RS.MoveNext
Loop
MsgBox ("There are:" & " " & RS.RecordCount & " " & "records in the database")
End If
RS.Close
Set RS = Nothing
我的 Access table 有 10 条记录和一个短文本字段。我正在使用 .recordcount 函数 return 此 table 中的记录数。代码如下:
Dim db as DAO.Database
Dim RS as DAO.Recordset
Dim recCount as Integer
Set db = CurrentDb
Set RS = db.OpenRecordset("Table Name")
RS.MoveFirst
RS.MoveLast
recCount = RS.recordcount
Debug.Print(recCount)
Dim i as Integer
i = 0
RS.MoveFirst
'Option one. Commented out when option two is active and vice verse
Do While i < 10
Debug.Print(RS(i))
i = i + 1
Loop
Do While i < 10
Debug.print(RS![Only Field Name])
i = i + 1
RS.MoveNext
Loop
recCount 始终打印为 0。尝试打印记录集中的记录将 return 仅是记录集中的第一个值,没有其他。读取第一条记录后,程序抛出错误 "Item not found in collection." 我不确定是什么导致了这个错误,因为我在另一个 VBA 模块中使用了与另一个 table 完全相同的方法,效果很好。
我在别处查看解决方案,我唯一能找到的是在打开后添加 RS.moveFirst
和 RS.moveLast
,但这不起作用。我认为这是因为打开的记录集实际上并不包含 table.
提前致谢。
编辑:
试试这个:
Sub Demo_IterateRecords()
Const tblName = "YOUR TABLE NAME HERE"
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(tblName)
With rs
.MoveLast
.MoveFirst
If MsgBox("Do you want to list all " & .RecordCount & " records?", _
vbOKCancel, "Confirmation") <> vbOK Then GoTo ExitMySub
Do While Not .EOF
Debug.Print .Fields(0), .Fields(1), .Fields(2)
rs.MoveNext
Loop
ExitMySub:
.Close
End With
Set rs = Nothing
End Sub
我使用 .Fields(_)
是因为我不确定您的字段叫什么,但更好的引用方式是按名称,例如:
Debug.Print !myID, !myEmployeeName, !myEmployeeAddress
原答案:
试试这个:
RS.MoveLast
RS.MoveFirst
recCount = RS.RecordCount
Debug.Print(recCount)
在您至少浏览一次记录之前,Access 不知道有多少条记录。
如果您在 循环后检查 RS.RecordCount
的值,您会得到一个数字。
Remarks
Use the
Recordcount
property to find out how many records in a Recordset orTableDef
object have been accessed. TheRecordCount
property doesn't indicate how many records are contained in a dynaset–, snapshot–, or forward–only–type Recordset object until all records have been accessed. Once the last record has been accessed, theRecordCount
property indicates the total number of undeleted records in the Recordset or TableDef object. To force the last record to be accessed, use the MoveLast method on the Recordset object. You can also use an SQLCount
function to determine the approximate number of records your query will return.Important Note
Using the
MoveLast
method to populate a newly opened Recordset negatively impacts performance. Unless it is necessary to have an accurateRecordCount
as soon as you open a Recordset, it's better to wait until you populate the Recordset with other portions of code before checking theRecordCount
property.
(Source)
另请参阅:MSDN:Recordset.RecordCount Property
我设法解决了这个问题,但我不知道为什么会这样。我没有创建新的 table 并输入十条记录的值,而是使用插入查询将我想要的查询值放入 table。使用这个新的 table,它成功了。
您可以边数边列出记录:
Set RS = db.OpenRecordset("Table Name")
While Not RS.EOF
Debug.Print RS![Only Field Name].Value
i = i + 1
RS.MoveNext
Loop
Debug.Print i & " records found."
也许你可以在你的子例程中尝试类似的东西:
Dim db As DAO.Database
Dim RS As DAO.Recordset
Dim recCount As Integer
Set db = CurrentDb
Set RS = db.OpenRecordset("Table Name")
If Not (RS.EOF And RS.BOF) Then
RS.MoveFirst
Do Until RS.EOF = True
RS.MoveNext
Loop
MsgBox ("There are:" & " " & RS.RecordCount & " " & "records in the database")
End If
RS.Close
Set RS = Nothing