How refer to the Query Column Names by code

Please help.

I have a crosstab query where the column names are changing every Year.

The report is for getting totals for past 3 years + the year I input.
eg. If I input 2001 the report will be for 1998, 1999, 2000 and 2001

I have created a crosstab query which I am referring to in codes below

Set rst = CurrentDb.OpenRecordset("select * FROM  Form6_Crosstab")

    If Not rst.EOF Then
            rst.MoveFirst
            Do Until rst.EOF
                           
                If IsNull(rst![NAME OF THE COLUMN]) = False Then

                'PY1 = PY1 + rst![NAME OF THE COLUMN]

                End If
                           
            rst.MoveNext
            Loop
                   
        End If

EndDtYr = Format(Forms![IARpts]![EndingDate], "YYYY")

CurrentYear = EndDtYr
Year1 = EndDtYr - 1 (year 1 before the input date)
Year2 = EndDtYr - 2 (year 2 before the input date)
Year3 = EndDtYr - 3 (year 3 before the input date)


EndDtYr is a text field where I enter the current year which the report is based on then I can get the past 3 year in the above code. The problem is the column name in the crosstab changes every time when I enter a new year. How can I refer to the column Names by code. eg. rst![Year1] etc. the name year1 will depends on the year I enter.

WHAT AM I DOING WRONG

Can any one Please help
mwaresysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlbertoFrogCommented:
You can create a string which can refer to the column name.

The recordset object has a fields collection attached to it, but since this is the default property of the object, you don't have to specify it.

E.g. rst("columnName") is equivalent to rst!columnName
(Or rst.Fields("columnName"))

This means that you can build the name in code, rather than fixing it a development time.

Alternatively you can use the same property but pass an integer instead, which will return the field in that ordinal position, starting from 0.

E.g. rst(0) or rst.Fields(0)


Alberto
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mwaresysAuthor Commented:
Thanks AlbertoFrog it worked perfect.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.