Connecting Tech Pros Worldwide Forums | Help | Site Map

Retrieving Data From a DAO Recordset Using GetRows()

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#1   Apr 2 '08
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows() Method of the Recordset Object. This Method varies slightly from DAO to ADO, so for purposes of this discussion, we'll be talking about DAO Recordsets. The ADO approach will be addressed in the following Tip. We'll be using a Query, consisting of 5 Fields, and based on the Employees Table of the sample Northwind Database, for both DAO and ADO discussions. The Recordset that we will be retrieving Rows from will be based on this Query.

The GetRows() Method copies Records from a Recordset and places them in a 2-dimensional Array. The first subscript identifies the Field, while the second identifies the Row. An outline of the GetRows() Method syntax is listed below:
Expand|Select|Wrap|Line Numbers
  1. varArray = Recordset.GetRows(numberofrows)
  2.     varArray - a Variant, 2-dimensional Array storing the returned data
  3.     recordset - an Object Variable representing a Recordset
  4.     numberofrows - a Variant indicating the number of Rows to retrieve
  1. Special Considerations involving the GetRows() Method
    1. If you request more Rows than are available, GetRows() returns only the number of available Rows.
    2. The UBound() Function is used to determine how many Rows are actually retrieved, because the Array is sized to fit the number of the returned Rows.
    3. Because GetRows() returns 'all' Fields of the Records, you may want to restrict the Fields returned in the Query.
    4. After you call the GetRows() Method, the Current Record is positioned at the next, unread, Row,
    5. The GetRows() Method will return fewer than the number of Rows requested in two cases: if the EOF has been reached, or if the Method tried to retrieve a Record that was deleted by another User.

Rather than going into a prolonged discussion of exactly how this Method works, I've decided to post a well documented code block. Hopefully, this code segment will illustrate the important aspects of this very useful Method. Should there be any questions whatsoever, please feel free to inquire or comment, and bring them up for discussion. I've also made the Test Database that I've used for this Tip available for download as an Attachment. Feel free to do whatever you like with it.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstEmployees As DAO.Recordset
  3. Dim varEmployees As Variant
  4. Dim intRowNum As Integer
  5. Dim intColNum As Integer
  6.  
  7. 'Make up of qryEmployees (5 Fields/9 Records) based on the
  8. 'sample Northwind.mdb Database
  9.   '[LastName] - Ascending
  10.   '[FirstName] - Ascending
  11.   '[Address]
  12.   '[City]
  13.   '[Region]
  14.  
  15. Set MyDB = CurrentDb
  16. Set rstEmployees = MyDB.OpenRecordset("qryEmployees", dbOpenSnapshot)
  17.  
  18. 'sometimes necessary for a valid Record Count
  19. rstEmployees.MoveLast
  20. rstEmployees.MoveFirst
  21.  
  22. 'Let's retrieve ALL Rows in the rstEmployees Recordset
  23. varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount)
  24.  
  25. 'If fewer than the desired number of Reows were returned
  26. If rstEmployees.RecordCount > UBound(varEmployees, 2) + 1 Then
  27.   MsgBox "Fewer Rows were returned than those requested"
  28. End If
  29.  
  30. 'Let's retrieve the first 6 Rows in the rstEmployees Recordset
  31. 'varEmployees = rstEmployees.GetRows(6)
  32.  
  33. '1st Row is the 0 Element of the Array, so we need the +1
  34. '2nd Subscript (2) identifies the Row Number
  35. Debug.Print "Number of Rows Retrieved: " & UBound(varEmployees, 2) + 1
  36.  
  37. Debug.Print
  38.  
  39. '1st Field is the 0 Element of the Array, so we need the +1
  40. '1st Subscript (1) identifies the Field
  41. Debug.Print "Number of Fields Retrieved: " & UBound(varEmployees, 1) + 1
  42.  
  43. Debug.Print
  44.  
  45. 'Let's retrieve the value of the 3rd Field ([Address]) in Row 5
  46. Debug.Print "Field 3 - Row 5: " & varEmployees(2, 4)
  47. 'Let's retrieve the value of the 1st Field ([LastName]) in Row 2
  48. Debug.Print "Field 1 - Row 2: " & varEmployees(0, 1)
  49.  
  50. Debug.Print
  51.  
  52. 'Debug.Print "******************************************"       'Column Format only
  53. Debug.Print "Last Name", "First Name", "Address", , "City", "Region"
  54. Debug.Print "---------------------------------------------------------------------------------------------"
  55. For intRowNum = 0 To UBound(varEmployees, 2)        'Loop thru each Row
  56.   For intColNum = 0 To UBound(varEmployees, 1)      'Loop thru each Column
  57.     'To Print Fields in Column Format with numbered Field and Row
  58.     'Debug.Print "Record#:" & intRowNum + 1 & "/Field#:" & intColNum + 1 & " ==> " & _
  59.                  'varEmployees(intColNum, intRowNum)
  60.     'To Print in Table Format, no numbered Fields or Rows
  61.     Debug.Print varEmployees(intColNum, intRowNum),
  62.   Next
  63.   Debug.Print vbCrLf
  64.   'Debug.Print "******************************************"     'Column Format only
  65. Next
  66.  
  67. rstEmployees.Close
  68. Set rstEmployees = Nothing
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Number of Rows Retrieved: 9
  2.  
  3. Number of Fields Retrieved: 5
  4.  
  5. Field 3 - Row 5: 908 W. Capital Way
  6. Field 1 - Row 2: Callahan
  7.  
  8. Last Name     First Name    Address                     City          Region
  9. ---------------------------------------------------------------------------------------------
  10. Buchanan      Steven        14 Garrett Hill             London        Null          
  11.  
  12. Callahan      Laura         4726 - 11th Ave. N.E.       Seattle       WA            
  13.  
  14. Davolio       Nancy         507 - 20th Ave. E., Apt. 2A Seattle       WA            
  15.  
  16. Dodsworth     Anne          7 Houndstooth Rd.           London        Null          
  17.  
  18. Fuller        Andrew        908 W. Capital Way          Tacoma        WA            
  19.  
  20. King          Robert        Edgeham Hollow, Winchester Way            London        Null          
  21.  
  22. Leverling     Janet         722 Moss Bay Blvd.          Kirkland      WA            
  23.  
  24. Peacock       Margaret      4110 Old Redmond Rd.        Redmond       WA            
  25.  
  26. Suyama        Michael       Coventry House, Miner Rd.   London        Null   



Member
 
Join Date: Oct 2006
Location: Virginia
Posts: 65
#2   Apr 3 '08

re: Retrieving Data From a DAO Recordset Using GetRows()


So would this be a good way to retrieve data from a crosstab query when you don't know for sure how many columns will be returned or what they will be named?

What are the reasons for using this method?

Thanks,
Jim
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#3   Apr 3 '08

re: Retrieving Data From a DAO Recordset Using GetRows()


Quote:

Originally Posted by jimatqsi

So would this be a good way to retrieve data from a crosstab query when you don't know for sure how many columns will be returned or what they will be named?

What are the reasons for using this method?

Thanks,
Jim

In an instant, GetRows() transfers the contents of a Recordset into a 2-Dimensional, Variant, Array for easy retrieval. To duplicate this functionality with traditional techniques would be much more laborious and time consuming.
Reply