RecordSet Clone RecordCount等于0?

时间:2014-02-03 21:30:04

标签: vba ms-access

我有点困惑为什么这没有找到记录数。我的表“tblDatabase”有3条记录。我想要它克隆记录集,以便我们实际上不会改变任何记录。我们只是想在读取数据时读取它们,然后进一步检查已检查的项目(在这种情况下,字段“SELECT”设置为True)。

谁能告诉我我做错了什么? RecordCount一直恢复为0.尝试使用RS1.MoveLast,MoveNext等...但它返回相同。我非常感谢您对我所做错事的任何帮助!

Set RS1 = CurrentDb.OpenRecordset("tblDatabase").Clone

    RS1.MoveFirst
    Debug.Print RS1.RecordCount

    For i = 1 To RS1.RecordCount
        If RS1.Fields("Select") = True Then

            strRequestNo = strRequestNo & IIf(Len(strRequestNo) = 0, "", ",") & Str(RS1.Fields("Request No"))
            strName = RS1.Fields("Name")

            'Pops up the Approval Dialog for user to Name + Date Approved.
            'The Code should not continue until the form is closed.
            DoCmd.OpenForm "frmClientAuthorization", acNormal, , , acFormEdit, acDialog, strRequestNo & "|" & strClientName


        End If
        RS1.MoveNext
    Next i

2 个答案:

答案 0 :(得分:0)

Please note that clone does not make a recordset read only.你真的不需要那个。

如果你想要一个只读记录集,我建议在打开记录集时使用dbReadOnly的RecordsetOptionEnum值:

How to open a record set

RecordsetOptionEnum

关于它返回零记录计数,您需要指定打开表类型记录集:

'Open a table-type Recordset
 Set rsTable = dbs.OpenRecordset("tblDatabase", dbOpenTable)

此外,(在我看来)这里有两种更优雅的方式来循环记录集:

Example 1

Example 2

HTH,

埃里克

答案 1 :(得分:0)

如果你使用“for i = ...”那么你需要做一个 rs1.movelast myvar = rs1.recordcount rs1.movefirst 用于访问知道记录集匹配的记录数。

在for-next循环中使用myvar变量

执行任务的更简洁方法如下:

RS1.MoveLast
Debug.Print RS1.RecordCount

Do Until rs1.EOF = True
    If RS1.Fields("Select") = True Then

        strRequestNo = strRequestNo & IIf(Len(strRequestNo) = 0, "", ",") & Str(RS1.Fields("Request No"))
        strName = RS1.Fields("Name")

        'Pops up the Approval Dialog for user to Name + Date Approved.
        'The Code should not continue until the form is closed.
        DoCmd.OpenForm "frmClientAuthorization", acNormal, , , acFormEdit, acDialog, strRequestNo & "|" & strClientName


    End If
    RS1.MoveNext
Loop