获取记录集的当前记录

时间:2016-01-25 01:18:29

标签: vba ms-access access-vba

我有一个对象employees的记录集,我可以使用.MoveX的方法移动。但是,我想知道如何使用记录集employee的当前记录创建对象employees

Dim employees As DAO.Recordset
Dim employee  As DAO.Recordset

Set employees = database.OpenRecordset("SELECT * FROM EMPLOYEES", dbOpenDynaset)
Do While Not employees.EOF

    // Some magic function 'GetCurrentRecord'
    employee = employees.GetCurrentRecord
    // I can do stuff with 'employee'
    MsgBox "Name: " & employee.Fields("Name").Value

    employees.MoveNext
Loop

2 个答案:

答案 0 :(得分:1)

从示例中不清楚为什么需要employee对象,您可以使用employees记录集在每个循环步骤中访问当前记录中的数据,但是如果您想要,例如,找到一些记录集行在循环中然后访问这些行,您可以使用Bookmark记录集属性来存储/恢复当前记录集位置。

答案 1 :(得分:0)

正如您自己提到的,显而易见的方法是使用该单个当前记录创建一个新的记录集:

Dim employees As DAO.Recordset
Dim employee  As DAO.Recordset

Set employees = database.OpenRecordset("SELECT * FROM EMPLOYEES", dbOpenDynaset)
Do While Not employees.EOF
    ' Specify condition when to extract the current record.
    If SomeCondition = True Then
        employees.Filter = "Id = " & employees!Id.Value & ""
        Set employee = employees.OpenRecordset
        Exit Do
    End If   
    employees.MoveNext
Loop

If Not employee Is Nothing Then
    ' I can do stuff with 'employee'
    MsgBox "Name: " & employee.Fields("Name").Value
End If