473,756 Members | 3,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving Data From an ADO Recordset Using GetRows()

ADezii
8,834 Recognized Expert Expert
[OVERVIEW]
Last Tip, we demonstrated the technique for retrieving data from a DAO Recordset, and placing it into a 2-dimensional Array using the GetRows() Method. This week, we will cover the same exact Method (GetRows()), but only as it applies to an ADO Recordset. Although there are similarities in the 2 methodologies, the ADO Method offers 2 more Optional Arguments, is a little more complex, and of course, the syntax is different in creating the Recordset. The differences will be noted here along with some similarities, if you wish to see a General Overview of GetRows(), please reference the previous Tip (#49).

[SYNTAX OF ADO GETROWS() VERSION]
  • array = Recordset.GetRo ws(Rows, Start, Fields)
    • Rows - (Optional), indicates the number of Records to retrieve, and defaults to all Records.
    • Start - (Optional), a String or Variant that evaluates to the Bookmark for the Record from which the GetRows() operation should begin.
    • Fields - (Optional), a Variant that represents a single Field Name, Ordinal Field position, Array of Field Names, or an Array of Ordinal Field positions. Only the data in these Fields are returned by GetRows()
[CODE EXAMPLE]
Expand|Select|Wrap|Line Numbers
  1. Dim rstEmployees As ADODB.Recordset
  2. Dim varEmployees As Variant
  3. Dim intRowNum As Integer
  4. Dim intColNum As Integer
  5.  
  6. 'Make up of qryEmployees (5 Fields/9 Records) based on the
  7. 'sample Northwind.mdb Database
  8.   '[LastName] - Ascending
  9.   '[FirstName] - Ascending
  10.   '[Address]
  11.   '[City]
  12.   '[Region]
  13.  
  14. Set rstEmployees = New ADODB.Recordset
  15.  
  16. With rstEmployees
  17.   .Source = "qryEmployees"
  18.   .ActiveConnection = CurrentProject.Connection
  19.   .CursorType = adOpenKeyset
  20.   .LockType = adLockOptimistic
  21.     .Open
  22.       .MoveLast
  23.       .MoveFirst
  24. End With
  25.  
  26. 'Let's retrieve ALL Rows in the rstEmployees Recordset
  27. varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount)
  28.  
  29. 'Demonstration of the Fields Parameter of GetRows()
  30.  
  31. 'Let's retrieve only the LastName Field in the rstEmployees Recordset
  32. 'varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount, , "LastName")
  33.  
  34. 'Let's retrieve only the 3rd Field ([Address]) in the rstEmployees Recordset
  35. 'varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount, , 2)
  36.  
  37. 'Let's retrieve the [Address], [City], and [Region Fields by passing an Array
  38. 'of these Field Names as the Fields Parameter
  39. 'Dim avarFieldNames
  40. 'avarFieldNames = Array("Address", "City", "Region")
  41. 'varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount, , avarFieldNames)
  42.  
  43. 'Let's retrieve the [Address], [City], and [Region Fields by passing an Array
  44. 'of the Ordinal Positions of these Fields to the Fields Parameter
  45. 'Dim avarFieldNames(1 To 3) As Variant
  46. 'avarFieldNames(1) = 2
  47. 'avarFieldNames(2) = 3
  48. 'avarFieldNames(3) = 4
  49. 'varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount, , avarFieldNames)
  50. '*****************************************************************
  51. 'Demonstration of the Bookmark Parameter of GetRows()
  52.  
  53. 'Retrieve only those Records from thew Current Bookmark on
  54. 'Dim varBkMrk As Variant
  55. 'rstEmployees.Find "[LastName]='King'"
  56. 'varBkMrk = rstEmployees.Bookmark
  57.  
  58. 'varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount, varBkMrk)
  59.  
  60. '*****************************************************************
  61. 'Let's retrieve the first 6 Rows in the rstEmployees Recordset
  62. 'varEmployees = rstEmployees.GetRows(6)
  63.  
  64. 'If fewer than the desired number of Reows were returned
  65. If rstEmployees.RecordCount > UBound(varEmployees, 2) + 1 Then
  66.   MsgBox "Fewer Rows were returned than those requested"
  67. End If
  68.  
  69. '1st Row is the 0 Element of the Array, so we need the +1
  70. '2nd Subscript (2) identifies the Row Number
  71. Debug.Print "Number of Rows Retrieved: " & UBound(varEmployees, 2) + 1
  72.  
  73. Debug.Print
  74.  
  75. '1st Field is the 0 Element of the Array, so we need the +1
  76. '1st Subscript (1) identifies the Field
  77. Debug.Print "Number of Fields Retrieved: " & UBound(varEmployees, 1) + 1
  78.  
  79. Debug.Print
  80.  
  81. 'Let's retrieve the value of the 3rd Field ([Address]) in Row 5
  82. Debug.Print "Field 3 - Row 5: " & varEmployees(2, 4)
  83. 'Let's retrieve the value of the 1st Field ([LastName]) in Row 2
  84. Debug.Print "Field 1 - Row 2: " & varEmployees(0, 1)
  85.  
  86. Debug.Print
  87.  
  88. 'Debug.Print "******************************************"       'Column Format only
  89. Debug.Print "Last Name", "First Name", "Address", , "City", "Region"
  90. Debug.Print "---------------------------------------------------------------------------------------------"
  91. For intRowNum = 0 To UBound(varEmployees, 2)        'Loop thru each Row
  92.   For intColNum = 0 To UBound(varEmployees, 1)      'Loop thru each Column
  93.     'To Print Fields in Column Format with numbered Field and Row
  94.     'Debug.Print "Record#:" & intRowNum + 1 & "/Field#:" & intColNum + 1 & " ==> " & _
  95.                  'varEmployees(intColNum, intRowNum)
  96.     'To Print in Table Format, no numbered Fields or Rows
  97.     Debug.Print varEmployees(intColNum, intRowNum),
  98.   Next
  99.   Debug.Print vbCrLf
  100.   'Debug.Print "******************************************"     'Column Format only
  101. Next
  102.  
  103. rstEmployees.Close
  104. Set rstEmployees = Nothing
[NOTE]
In order to simplify matters for everyone, I've made the sample Database that I used to create both Tips #49 and #50, (DAO and ADO GetRows() Method), available as an Attachment for anyone who is interested to download.
Apr 17 '08 #1
3 43527
sal21
27 New Member
@ADezii
i friend i am very interesting to yuor code...please send me a copy of your project. gss.italyATiol. it
Many tks.
Sal
Jan 31 '09 #2
ADezii
8,834 Recognized Expert Expert
@sal21
There is no Project to send. Everything you need codewise is in Post #9.
Feb 1 '09 #3
sal21
27 New Member
@ADezii
please link of post#9 i dont see that(!?);-(
Feb 1 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1782
by: John M | last post by:
Hi, Having struggled to master (or at least get to use) recordsets, and so retrieve data without using a form, I'm stuck at a point which I feel ought to be easy. MsgBox Rs.Myfield (with brackets, without, with ! or .) will produce a result, so my recordset (rs) is fine. Now I want to choose the field through a function, so that the field name is
5
2235
by: aniket_sp | last post by:
i am using a data adapter and a dataset for filling and retrieving data into .mdb database. following is the code..... for the form load event Dim dc(0) As DataColumn Try If OleDbConnection1.State = ConnectionState.Closed Then OleDbConnection1.Open() Else
1
5725
by: stjulian | last post by:
If inside a stored procedure, there a SELECT statement to return a recordset and another SELECT to set the value of an output parameter (as in SELECT @OutValue = Name FROM table WHERE pkid=5), would 2 execute statements be needed to return the OUTPUT parameter? Like this? adocmd.CommandTimeout = 120 adocmd.ActiveConnection = conn
0
2073
by: Andy | last post by:
Hi All. I'm working for a company that has set out a guideline for retrieving data from a database. Nobody can explain to me the reason for the following. When retrieving a set of records from database to a VB.net app, they retrieve the database fields as a record set eg. "select name, suburb from myTable"
3
3654
by: Jakob Petersen | last post by:
Hi, I need to increase the speed when retrieving data from a hosted SQL Server into VBA. I'm using simple SELECT statements. How important is the speed of my Internet connection? (I have 4mbits) Should I index my tables or use Stored Procedures? Or is there a kind of "flush" function or readonly function or... Or is it simply a question of the amount of data transmitted over the Internet?
0
1529
by: DC01 | last post by:
I have added a new measure successfully into the normal cube. I then add it to the virtual cube and reprocess all cubes. I can browse the normal cube successfully. Then when I try and browse the virtual cube it says 'Retrieving Data' but then thats it, it just hangs like that. I am using AS2000. The other problem I have is that the data is doubling up when its in the cube. The SQL which sets up the fact tables are all fine and are...
4
18818
by: smartin | last post by:
Hi, I'm having problem retrieving data from an SQL stored procedure. I tried debugging but it wont give a the reason for the error. it just throws an exception after executing cmd.ExecuteNonQuery without any details. Can anyone please help me.. Im stuck on it since 2 days Thanks Stored Procedure
9
35539
ADezii
by: ADezii | last post by:
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...
3
3950
by: Executable | last post by:
Hi everyone, I have a question. I am creating a dynamic report from a table and need to fetch all the data (values and column names) into an array so that I can manipulate the data. I know there is a way to get the values in the recordset using GetRows() but that Only gives me the values, I also need the Field names of the columns. Does anybody know how to do this? Thanks,
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.