简体   繁体   English

当记录存在时,MS Access VBA .recordcount返回0,而debug.print返回值

[英]MS Access VBA .recordcount returning 0 when records exist, and debug.print returns value

I have an Access table with 10 records and one field of short text. 我有一个包含10条记录和一个短文本字段的Access表。 I am using the .recordcount function to return the number of records in this table. 我正在使用.recordcount函数返回此表中的记录数。 Code below: 代码如下:

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 always prints out to be 0. Attempting to print out the records in the recordset will return the first value only of the recordset and nothing else. recCount始终打印为0。尝试打印记录集中的记录将仅返回记录集的第一个值,而不会返回其他任何值。 After reading the first record, the program throws the error "Item not found in collection." 读取第一条记录后,程序将引发错误“在集合中找不到项目”。 I'm unsure of what could be causing this error, as I use the exact same method with another table in another VBA module, which works just fine. 我不确定是什么原因导致此错误,因为我对另一个VBA模块中的另一个表使用了完全相同的方法,效果很好。

I look at solutions to this elsewhere and the only one I could find was to add a RS.moveFirst and RS.moveLast after opening, however this does not work. 我在其他地方查看了解决方案,我唯一能找到的解决方案是在打开后添加RS.moveFirstRS.moveLast ,但这是行不通的。 I think this is becuase the opened recordset does not actually contain all the records in the table. 我认为这是因为打开的记录集实际上并不包含表中的所有记录。

Thanks in advance. 提前致谢。

You could list the records while counting: 您可以在计数时列出记录:

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."

EDIT: 编辑:

Try this: 尝试这个:

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

I used .Fields(_) because I'm not sure what your fields are called, but a better way to refer to them would be by name, like: 我使用.Fields(_)是因为我不确定您的字段叫什么,但是引用它们的更好方法是按名称命名,例如:

        Debug.Print !myID, !myEmployeeName, !myEmployeeAddress

Original Answer: 原始答案:

Try this: 尝试这个:

RS.MoveLast
RS.MoveFirst
recCount = RS.RecordCount
Debug.Print(recCount)

Access doesn't know how many records there are until you move through them at least once. Access不知道有多少条记录,除非您至少遍历一次。

If you would have checked the value of RS.RecordCount after your loop, you would have got a number. 如果循环检查了RS.RecordCount的值, RS.RecordCount获得一个数字。

Remarks 备注

Use the Recordcount property to find out how many records in a Recordset or TableDef object have been accessed. 使用Recordcount属性来查找Recordset或TableDef对象中已访问了多少条记录。 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. 在已访问所有记录之前, RecordCount属性不会指示动态集,快照或仅向前类型的Recordset对象中包含多少条记录。 Once the last record has been accessed, the RecordCount property indicates the total number of undeleted records in the Recordset or TableDef object. 一旦访问了最后一条记录, RecordCount属性将指示Recordset或TableDef对象中未删除记录的总数。 To force the last record to be accessed, use the MoveLast method on the Recordset object. 若要强制访问最后一条记录,请在Recordset对象上使用MoveLast方法。 You can also use an SQL Count function to determine the approximate number of records your query will return. 您还可以使用SQL Count函数来确定查询将返回的大约记录数。

Important Note 重要的提示

Using the MoveLast method to populate a newly opened Recordset negatively impacts performance. 使用MoveLast方法填充新打开的Recordset会对性能产生负面影响。 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. 除非有必要在打开Recordset时立即获得准确的RecordCount ,否则最好等到用其他代码部分填充Recordset,然后再检查RecordCount属性。

( Source ) 来源


See also: MSDN : Recordset.RecordCount Property 另请参见: MSDN: Recordset.RecordCount属性

I managed to fix this issue but I have no idea why this worked. 我设法解决了这个问题,但我不知道为什么会这样。 Instead of creating a new table and typing in the values for the ten records, I instead used an insert query to put the values I wanted from a query into a table. 我没有创建新表并输入十个记录的值,而是使用插入查询将查询所需的值放入表中。 Using this new table, it worked. 使用这个新表,它起作用了。

Perhaps you could try something similar to this in your sub routine: 也许您可以在子例程中尝试类似的操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM