使用 DAO 控制多值字段

多值字段在 DAO 中表示为 Recordset 对象。 字段的记录集是包含多值字段的表的记录集的子集。 若要实例化子记录集,请使用多值字段的 Value 属性,如下所示。

Set childRs = rs.<multi-valued field>.Value

下面的代码示例演示如何实例化 Tasks 表的 AssignedTo 字段的子记录集。

Set rs  = db.OpenRecordSet("Tasks") 
Set childRs = rs.AssignedTo.Value 

子记录集具有与任何 DAO Recordset 对象相同的功能。

下面的代码示例演示如何循环访问父记录集及其子记录集。 该示例在即时窗口中显示 Tasks 表中的任务以及指派给这些任务的人员。

Sub BrowseMultiValueField() 
   Dim db As Database 
   Dim rs As Recordset 
   Dim childRS As Recordset 
     
   Set db = CurrentDb() 
     
   ' Open a Recordset for the Tasks table. 
   Set rs = db.OpenRecordset("Tasks") 
   rs.MoveFirst 
     
   Do Until rs.EOF 
      ' Print the name of the task to the Immediate window. 
      Debug.Print rs!TaskName.Value 
         
      ' Open a Recordset for the multivalued field. 
      Set childRS = rs!AssignedTo.Value 
 
         ' Exit the loop if the multivalued field contains no records. 
         Do Until childRS.EOF 
             childRS.MoveFirst 
                     
             ' Loop through the records in the child recordset. 
             Do Until childRS.EOF 
                 ' Print the owner(s) of the task to the Immediate  
                 ' window. 
                 Debug.Print Chr(0), childRS!Value.Value 
                 childRS.MoveNext 
             Loop 
         Loop 
      rs.MoveNext 
   Loop 
End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。