Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Recordset using a variable

Posted on 2011-03-02
3
Medium Priority
?
321 Views
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.

Thanks,
0
Comment
Question by:bvanee1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
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
Loop
oldDateResult = rs(x)

HTH,

MV
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 35021692
Try:

rs.Fields(TheColumnNameIWant)
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 2000 total points
ID: 35021775


rs(YourStringVariableName)

eg.

Dim sMyFldName, x

sMyFldName="ThatField"
x=rs(sMyFldName)

mx

mx
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

721 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