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] -
Dim rstEmployees As ADODB.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 rstEmployees = New ADODB.Recordset
-
-
With rstEmployees
-
.Source = "qryEmployees"
-
.ActiveConnection = CurrentProject.Connection
-
.CursorType = adOpenKeyset
-
.LockType = adLockOptimistic
-
.Open
-
.MoveLast
-
.MoveFirst
-
End With
-
-
'Let's retrieve ALL Rows in the rstEmployees Recordset
-
varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount)
-
-
'Demonstration of the Fields Parameter of GetRows()
-
-
'Let's retrieve only the LastName Field in the rstEmployees Recordset
-
'varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount, , "LastName")
-
-
'Let's retrieve only the 3rd Field ([Address]) in the rstEmployees Recordset
-
'varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount, , 2)
-
-
'Let's retrieve the [Address], [City], and [Region Fields by passing an Array
-
'of these Field Names as the Fields Parameter
-
'Dim avarFieldNames
-
'avarFieldNames = Array("Address", "City", "Region")
-
'varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount, , avarFieldNames)
-
-
'Let's retrieve the [Address], [City], and [Region Fields by passing an Array
-
'of the Ordinal Positions of these Fields to the Fields Parameter
-
'Dim avarFieldNames(1 To 3) As Variant
-
'avarFieldNames(1) = 2
-
'avarFieldNames(2) = 3
-
'avarFieldNames(3) = 4
-
'varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount, , avarFieldNames)
-
'*****************************************************************
-
'Demonstration of the Bookmark Parameter of GetRows()
-
-
'Retrieve only those Records from thew Current Bookmark on
-
'Dim varBkMrk As Variant
-
'rstEmployees.Find "[LastName]='King'"
-
'varBkMrk = rstEmployees.Bookmark
-
-
'varEmployees = rstEmployees.GetRows(rstEmployees.RecordCount, varBkMrk)
-
-
'*****************************************************************
-
'Let's retrieve the first 6 Rows in the rstEmployees Recordset
-
'varEmployees = rstEmployees.GetRows(6)
-
-
'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
-
-
'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
[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.
3 43527 @ADezii
i friend i am very interesting to yuor code...please send me a copy of your project. gss.italyATiol. it
Many tks.
Sal
ADezii 8,834
Recognized Expert Expert @sal21
There is no Project to send. Everything you need codewise is in Post #9.
@ADezii
please link of post#9 i dont see that(!?);-(
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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
|
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"
|
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?
| |
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...
|
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
|
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...
|
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,
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |