Recordset using a variable

Posted on 2011-03-02
Last Modified: 2013-12-25
I have a recordset based off a select query that work fine.  Note:  It will only ever return one record.
When I try to get the data from the record set it works fine if I explicitly tell it the column name as below:
oldDateResult = rs![The Column Name I Want]
But I have a variable which contains the column name which I need to use,
colName = "[The Column Name I want]"
oldDateResult = rs!colName
This doesn't work, says it can't find the column.  I hope I explained it good enough, just need to know how to reference the column name by the use of a variable.

Question by:bvanee1
LVL 10

Expert Comment

by:Michael Vasilevsky
ID: 35021560
I don't believe you can dynamically reference a recordset field like that, but you could reference it by index. I.e. you can find out the column with a loop and then reference the index. Just make sure the column name you want exists otherwise you'll have an endless loop!

x = 0
Do Until rst(x).Name = "[The Column Name I want]"
     x = x + 1
oldDateResult = rs(x)


LVL 14

Expert Comment

ID: 35021692

LVL 75

Accepted Solution

DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
ID: 35021775



Dim sMyFldName, x




Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to Classify of email (pdf attachments) 7 36
Create report using crosstab query 11 29
update all email addresses SQL 1 24
Filter a form 8 15
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question