以下范例说明了访问分级 Recordset 中的行的所需步骤:
(StayInSync 属性是为了说明而设置为 FALSE 的,以便您可以在每次外循环中显性地看见子集更改。但是,如果在步骤 3 中的赋值被移动到步骤 2 第一行之前,范例将会更有效,所以赋值只执行一次。然后将 StayInSync 属性设为 TRUE,这样无论 rst 何时移动到新行,rstTitleAuthor 都将隐性和自动地更改为相应的子集。)
范例
Sub datashape() Dim cnn As New ADODB.Connection Dim rst As New ADODB.Recordset Dim rstTitleAuthor As New ADODB.Recordset cnn.Provider = "MSDataShape" cnn.Open "Data Provider=MSDASQL;" & _ "DSN=vfox;uid=sa;pwd=vfox;database=pubs” ‘步骤 1 rst.StayInSync = FALSE rst.Open "SHAPE {select * from authors} APPEND ({select * from titleauthor} RELATE au_id TO au_id) AS chapTitleAuthor", cnn ‘步骤 2 While Not rst.EOF Debug.Print rst("au_fname"), rst("au_lname"), rst("state"), rst("au_id") ‘步骤 3 Set rstTitleAuthor = rst("chapTitleAuthor").Value ‘步骤 4 While Not rstTitleAuthor.EOF Debug.Print rstTitleAuthor(0), rstTitleAuthor(1), rstTitleAuthor(2), rstTitleAuthor(3) rstTitleAuthor.MoveNext Wend rst.MoveNext Wend End Sub