473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,387 developers and data experts.

Accessing Field Values in Recordsets

ADezii
8,834 Expert 8TB
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.
Apr 20 '07 #1
9 75881
NeoPa
32,556 Expert Mod 16PB
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.
Apr 26 '07 #2
ADezii
8,834 Expert 8TB
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.
Apr 26 '07 #3
@ADezii - thanks for the article. I, too, have a use for the Index method. When you have a query which results in Sums, Averages, Counts or other calculated values, it can be challenging to retrieve the values. The index syntax is fast and easy in these situations.
Oct 21 '11 #4
ADezii
8,834 Expert 8TB
@RisoSystems:
Good point. It also comes in handy when you have a Generic, Portable, Routine that will accept a Recordset Object and iterate the Fields in some manner. To be Portable, you must use the Index Method.
Oct 21 '11 #5
I've been using MyRS("LastName"). Is that a valid 5th method?
Oct 2 '16 #6
Oralloy
988 Expert 512MB
This may sound lame, but the "String" and "Variable" methods appear to me as fundamentally identical; the only difference is the source of the string. What am I missing?
Oct 2 '16 #7
ADezii
8,834 Expert 8TB
but the "String" and "Variable" methods appear to me as fundamentally identical; the only difference is the source of the string.
Agree with you wholeheartedly.
Oct 2 '16 #8
Oralloy
988 Expert 512MB
As an observation, looking up fields by index is highly useful, when you have large Data-Sets to work through. This can provide a noticeable throughput improvement (time savings).

Another by index lookup is when you have to perform a process against a query that you know nothing about, for example converting a query result into table in a web page. Or, for the brave, inserting a table into a uSoft-Word document.

The recipe is something of the sort:
  1. Execute your Query.
  2. Retrieve the RecordSet.
  3. Get indices for your required fields.
  4. Use indices instead of string-match look-up.
  5. Do something with the data.
Oct 3 '16 #9
Method #3 is the only one that works with multi-value fields. Thanks, that was a lifesaver!
Apr 3 '19 #10

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

Similar topics

2
by: Wishing I was skiing mom | last post by:
In form1, which is an item master maintenance screen, I have a button which launches form2. Form2 contains an item master datagrid. After the user double-clicks the desired item record in form2 I...
1
by: Megan | last post by:
I have 2 tables, CASE and ISSUE. My table, CASE, stores the issues pertaining to that particular case. The table, CASE, was designed with 2 fields, IssueID and IssueDesc. Right now, all of the...
1
by: Richard Dixson | last post by:
Under ASP.NET (C#) I want to create a page that people can use to submit questions. This page will consist of a table with several rows of information, like: Name: Bill Smith Job title:...
2
by: Richard Dixson | last post by:
Under ASP.NET (C#) I want to create a page that people can use to submit questions. This page will consist of a table with several rows of information, like: Name: Bill Smith Job title:...
0
by: rlueneberg | last post by:
I am having difficulty with preserving field values in a DetailsView control after clicking the insert button event. protected void DetailsView1_ItemInserted(object sender,...
5
by: mehere | last post by:
Please excuse me if I am posting in wrong NG. Can someone explain how I can use javascript (I am assuming this is the best way to do it) to add two field values together. I.e. field 1 plus field...
5
by: sklett | last post by:
I'm not real experienced with asp.net so this may be obvious. I've got a situation where some of my client side javascript is causing my posted form's controls to lose their values. In other...
3
by: Bjorn Sagbakken | last post by:
What is the best way to read single field values from a dataset? The dataset is populated with a customer record, showing well in a gridview. But I also want to read the single field values, like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...

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.