Connecting Tech Pros Worldwide Forums | Help | Site Map

Accessing Field Values in Recordsets

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#1   Apr 21 '07
There are basically 4 Methods that you can use to access the values of Fields in Recordsets. For the purposes of this demonstration, I'll be using a Table called tblEmployees, a Field named [LastName], and a DAO Recordset although these Methods are equally applicable to ADO Recordsets. The following code will print all the Last Name ([LastName]) values in the Employees (tblEmployees) Table using all 4 of these Methods.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  2. Dim strFieldName As String
  3.  
  4. strFieldName = "LastName"
  5.  
  6. Set MyDB = CurrentDb()
  7. Set MyRS = MyDB.OpenRecordset("tblEmployees", dbOpenForwardOnly)
  8.  
  9. Do While Not MyRS.EOF
  10.   'Method 1 - the Bang or Exclamation Method
  11.   Debug.Print MyRS![LastName]
  12.  
  13.   'Method 2 - the String Method
  14.   Debug.Print MyRS.Fields("LastName")
  15.  
  16.   'Method 3 - the Index Method (not recommended - see Note #2)
  17.   Debug.Print MyRS.Fields(2)
  18.  
  19.   'Method 4 - the Variable Method
  20.   Debug.Print MyRS.Fields(strFieldName)
  21.   MyRS.MoveNext
  22. Loop
  23.  
  24. MyRS.Close 
  25.  
  26. NOTE: All 4 Methods will produce the same results.
NOTE #2:
By using an Index to retrieve a Field's value in a Recordset, you are referring to a position number (Index), of an Object (Field), in its Collection (Fields). This Index number starts at 0 and ends with the number of items in the Collection - 1. Since [LastName] is the 3rd Field defined in tblEmployees, an Index of 2 (zero based) will be used to retrieve its value. As Objects are added to, or removed from a Collection, the position of other Objects may be affected because Access automatically updates Index numbers when a Collection changes. For this reason, the Index Method should not be used for retrieving Field values in Recordsets.



NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#2   Apr 26 '07

re: Accessing Field Values in Recordsets


Accessing a field via its index can be a very important concept nevertheless (hence its inclusion).

Consider the situation where you have to output all fields from a record, but you don't want to tie your code down to specific field names for reasons of reusability of code, or even simply that the record layout for you table is prone to change over time.
In this case, a loop incrementing the index number (or even a For Each {variable} In {collection) type loop) would give you the required results.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#3   Apr 26 '07

re: Accessing Field Values in Recordsets


Quote:

Originally Posted by NeoPa

Accessing a field via its index can be a very important concept nevertheless (hence its inclusion).

Consider the situation where you have to output all fields from a record, but you don't want to tie your code down to specific field names for reasons of reusability of code, or even simply that the record layout for you table is prone to change over time.
In this case, a loop incrementing the index number (or even a For Each {variable} In {collection) type loop) would give you the required results.

Good point, Neo.
Reply


Similar Microsoft Access / VBA bytes