IT tutorials
 
Office
 

Microsoft Access 2010 : DATA ACCESS WITH ADO (part 3) - Navigating Recordsets

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Product Key Free : Microsoft Office 2019 – Serial Number
11/21/2011 10:08:29 AM

4. Navigating Recordsets

Recordset objects are a collection of Record objects. The current record cursor defines the Record object that the Recordset is currently pointing at. Therefore, navigating through the records is necessary to gather the data from any given Record. A number of methods are available in the Recordset object to complete this task.

4.1. The Move Methods

When a Recordset object is created, and it has records, the current record cursor is automatically moved to the first record. To move forward (and backward, if supported by the cursor) through the records in the Recordset, there are five basic methods, each of which is listed and described in Table 2 below.

Table 2. The Move Methods of a Recordset Object
METHODDESCRIPTION
MoveMoves the cursor to the specified record.
MoveFirstMoves the cursor to the first record.
MoveNextMoves the cursor to the next record.
MovePreviousMoves the cursor to the previous record.
MoveLastMoves the cursor to the last record.

The following code provides an example of navigating records:

Public Sub NavigateRecords()

' Define Variables
Dim rs As New ADODB.Recordset
Dim results As String

' Get a Recordset from one of our previous example functions
Set rs = OpenRecordsetFromSql

' Move through the Recordset
results = results & "Name: " & rs(3).Value & vbNewLine
rs.MoveNext
results = results & "Name: " & rs(3).Value & vbNewLine
rs.MovePrevious
results = results & "Name: " & rs(3).Value & vbNewLine
rs.MoveLast
results = results & "Name: " & rs(3).Value & vbNewLine
rs.MoveFirst
results = results & "Name: " & rs(3).Value & vbNewLine

' Show the results
MsgBox results

End Sub


If the cursor is moved before the first record or after the last record, the BOF and EOF properties are set, respectively.

4.2. The Seek Method

The Seek method is the fastest way to find a specific record, but it can only be used with server-side cursors on tables that have been opened as adCmdTableDirect because it specifically relies on the table's indexes (and the indexes reside on the server — not on the client). Naturally, the table must have at least one index for it to search on. And, unfortunately, it is not supported by all providers.

To use the ADO Seek method, three items must be specified: The name of the index key to use, a variant array whose members specify the values to be compared with the key columns, and a SeekEnum constant that defines the kind of Seek to execute. The Recordset object's Index property must be set prior to calling the Seek method.

Public Sub SeekRecord()

' Define Variables
Dim rs As New ADODB.Recordset
Dim strSQL As String

' Open the Recordset
strSQL = "SELECT [CONTACTS].* FROM [CONTACTS]"
rs.Open strSQL, "<Connection to Seek Supported Provider>"

' Seek to the record we want
rs.Index = "ID"
rs.Seek 4, adSeekFirstEQ

' Show the results
MsgBox "Name: " & rs(3).Value

End Sub


If the method finds a record that matches the criteria, the Recordset object's cursor is moved to that row, and if not, to the end of the recordset. So, if no matching record is found, the Recordset object's EOF property is set to True.


4.3. The Find Method

The Recordset.Find method can also be used to navigate to a specific record. The Find method takes four parameters: Criteria, SkipRows, SearchDirection, and Start, but only the Criteria is required. Unless otherwise specified, all searches begin at the current row.

Public Sub FindRecord()

' Define Variables
Dim rs As New ADODB.Recordset

' Open the Recordset
rs.Open "Contacts", CurrentProject.Connection

' Find the record we want
rs.Find "[Last Name] = 'Griffith'"

' Show the results
MsgBox "Name: " & rs("First Name").Value

End Sub


Once the record is found, the current record cursor is moved to the record, as shown in the last example.

There are two other interesting points to note about the Find method. First, literal string values for the Criteria parameter can be specified either within single quotes or within hash characters. For example:

"State = 'NY'" or "State = #NY#"

Also, the use of the asterisk as a wildcard character in the Criteria parameter is restricted. It be can specified at the end of the criteria string, or at the beginning and end. But, the asterisk cannot be at the beginning only (without one also being at the end), or in the middle. Table 3 illustrates this point.

Table 3. Use of the Asterisk Operator for the Criteria Parameter
EXAMPLEVALIDITY
State LIKE '*York'Illegal
State LIKE 'New*'OK
State LIKE '*ew Yor*'OK
State LIKE 'New *ork'Illegal

4.4. The AbsolutePosition Property

Assuming the provider supports absolute positioning, the AbsolutePosition property enables you to move the cursor to a specific row in the Recordset. For example, to move to the 127th row, issue the following call:

rs.AbsolutePosition = 127

ADO provides three constants in the CursorPositionEnum to verify the cursor position:

  • adPosUnknown: The recordset is empty, or the provider doesn't support absolute positioning.

  • adPosBOF: True if the current cursor position is before the first record.

  • adPosEOF: True if the current cursor position is after the last record.

The AbsolutePosition property will be set to one of these values if the current record cursor does not point to a valid record.

 
Others
 
- Microsoft Access 2010 : DATA ACCESS WITH ADO (part 2) - Creating Recordsets
- Microsoft Access 2010 : DATA ACCESS WITH ADO (part 1) - Using the Execute Method
- Microsoft Word 2010 : Adding Supplementary Elements - Creating a Bibliography
- Microsoft Word 2010 : Adding Supplementary Elements - Figure Captions & Adding a Table of Figures
- Microsoft Visio 2010 : Tips for Creating Organizational Charts
- Microsoft Visio 2010 : Tips for Creating Timelines
- Microsoft PowerPoint 2010 : Prepare for Delivery - Rehearsing Presentations
- Microsoft PowerPoint 2010 : Prepare for Delivery - Adapting Presentations for Different Audiences
- Microsoft Excel 2010 : Analyzing Worksheet Data - Creating Groups and Outlines, Converting Text to Columns
- Microsoft Excel 2010 : Analyzing Worksheet Data - Charting a PivotTable
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us