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. - Dim MyDB As DAO.Database, MyRS As DAO.Recordset
-
Dim strFieldName As String
-
-
strFieldName = "LastName"
-
-
Set MyDB = CurrentDb()
-
Set MyRS = MyDB.OpenRecordset("tblEmployees", dbOpenForwardOnly)
-
-
Do While Not MyRS.EOF
-
'Method 1 - the Bang or Exclamation Method
-
Debug.Print MyRS![LastName]
-
-
'Method 2 - the String Method
-
Debug.Print MyRS.Fields("LastName")
-
-
'Method 3 - the Index Method (not recommended - see Note #2)
-
Debug.Print MyRS.Fields(2)
-
-
'Method 4 - the Variable Method
-
Debug.Print MyRS.Fields(strFieldName)
-
MyRS.MoveNext
-
Loop
-
-
MyRS.Close
-
-
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.
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.
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.
@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.
@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.
I've been using MyRS("LastName"). Is that a valid 5th method?
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?
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.
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: - Execute your Query.
- Retrieve the RecordSet.
- Get indices for your required fields.
- Use indices instead of string-match look-up.
- Do something with the data.
Method #3 is the only one that works with multi-value fields. Thanks, that was a lifesaver!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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:...
|
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:...
|
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,...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |