我有一个对象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
答案 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