Solved

Recordset using a variable

Posted on 2011-03-02
3
314 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
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 - Access MVP) earned 500 total points
ID: 35021775


rs(YourStringVariableName)

eg.

Dim sMyFldName, x

sMyFldName="ThatField"
x=rs(sMyFldName)

mx

mx
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now