Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Recordset using a variable

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

876 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