在TheScripts上经常出现的一个问题是:“创建记录集后,如何从记录集中检索数据?” Recordset对象的GetRows()方法是一种非常有效且不常用的方法。 从DAO到ADO,此方法略有不同,因此出于讨论目的,我们将讨论DAO记录集。 下面的技巧将介绍ADO方法。 对于DAO和ADO,我们将使用一个由5个字段组成的查询,该查询基于示例Northwind数据库的Employees表。 我们将从中检索行的Recordset将基于此查询。
GetRows()方法从Recordset复制记录,并将它们放置在二维数组中。 第一个下标标识字段,而第二个下标标识行。 下面列出了GetRows()方法语法的概述:
varArray = Recordset.GetRows(numberofrows)
varArray - a Variant, 2-dimensional Array storing the returned data
recordset - an Object Variable representing a Recordset
numberofrows - a Variant indicating the number of Rows to retrieve
- 涉及GetRows()方法的特殊注意事项
- 如果请求的行数多于可用行数,则GetRows()仅返回可用行数。
- UBound()函数用于确定实际检索到的行数,因为Array的大小适合于返回的行数。
- 由于GetRows()返回记录的“所有”字段,因此您可能希望限制查询中返回的字段。
- 调用GetRows()方法后,当前记录将位于下一个未读取的行,
- 在两种情况下,GetRows()方法返回的行数少于请求的行数:如果已达到EOF,或者该方法尝试检索由另一个用户删除的记录。
与其深入讨论此方法的确切工作方式,不如我决定发布一个有据可查的代码块。 希望这段代码将说明此非常有用的方法的重要方面。 如有任何疑问,请随时询问或发表评论,并进行讨论。 我还将本技巧所使用的测试数据库作为附件下载。 随意做任何您想做的事。
Dim MyDB As DAO.Database
Dim rstEmployees As DAO.Recordset
Dim varEmployees As Variant
Dim intRowNum As Integer
Dim intColNum As Integer
'Make up of qryEmployees (5 Fields/9 Records) based on the
'sample Northwind.mdb Database
'[LastName] - Ascending
'[FirstName] - Ascending
'[Address]
'[City]
'[Region]
Set MyDB = CurrentDb
Set rstEmployees = MyDB.OpenRecordset("qryEmployees", dbOpenSnapshot)
'sometimes necessary for a valid Record Count
rstEmployees.MoveLast
rstEmployees.MoveFirst
'Let's retrieve ALL Rows in the rstEmployees Recordset
varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount)
'If fewer than the desired number of Reows were returned
If rstEmployees.RecordCount > UBound(varEmployees, 2) + 1 Then
MsgBox "Fewer Rows were returned than those requested"
End If
'Let's retrieve the first 6 Rows in the rstEmployees Recordset
'varEmployees = rstEmployees.GetRows(6)
'1st Row is the 0 Element of the Array, so we need the +1
'2nd Subscript (2) identifies the Row Number
Debug.Print "Number of Rows Retrieved: " & UBound(varEmployees, 2) + 1
Debug.Print
'1st Field is the 0 Element of the Array, so we need the +1
'1st Subscript (1) identifies the Field
Debug.Print "Number of Fields Retrieved: " & UBound(varEmployees, 1) + 1
Debug.Print
'Let's retrieve the value of the 3rd Field ([Address]) in Row 5
Debug.Print "Field 3 - Row 5: " & varEmployees(2, 4)
'Let's retrieve the value of the 1st Field ([LastName]) in Row 2
Debug.Print "Field 1 - Row 2: " & varEmployees(0, 1)
Debug.Print
'Debug.Print "******************************************" 'Column Format only
Debug.Print "Last Name", "First Name", "Address", , "City", "Region"
Debug.Print "---------------------------------------------------------------------------------------------"
For intRowNum = 0 To UBound(varEmployees, 2) 'Loop thru each Row
For intColNum = 0 To UBound(varEmployees, 1) 'Loop thru each Column
'To Print Fields in Column Format with numbered Field and Row
'Debug.Print "Record#:" & intRowNum + 1 & "/Field#:" & intColNum + 1 & " ==> " & _
'varEmployees(intColNum, intRowNum)
'To Print in Table Format, no numbered Fields or Rows
Debug.Print varEmployees(intColNum, intRowNum),
Next
Debug.Print vbCrLf
'Debug.Print "******************************************" 'Column Format only
Next
rstEmployees.Close
Set rstEmployees = Nothing
输出:
Number of Rows Retrieved: 9
Number of Fields Retrieved: 5
Field 3 - Row 5: 908 W. Capital Way
Field 1 - Row 2: Callahan
Last Name First Name Address City Region
---------------------------------------------------------------------------------------------
Buchanan Steven 14 Garrett Hill London Null
Callahan Laura 4726 - 11th Ave. N.E. Seattle WA
Davolio Nancy 507 - 20th Ave. E., Apt. 2A Seattle WA
Dodsworth Anne 7 Houndstooth Rd. London Null
Fuller Andrew 908 W. Capital Way Tacoma WA
King Robert Edgeham Hollow, Winchester Way London Null
Leverling Janet 722 Moss Bay Blvd. Kirkland WA
Peacock Margaret 4110 Old Redmond Rd. Redmond WA
Suyama Michael Coventry House, Miner Rd. London Null
From: https://bytes.com/topic/access/insights/789969-retrieving-data-dao-recordset-using-getrows