MS Access 2013 convert rows to columns

I am trying to manipulate data in a table. Please see example below:
I have tried cross tab query but I cant get that to show all files on one row.

Current Table:

ID              ITEM               File    
1           30092-61         R30092-1
2           30092-61         R30092-2
3           30096-41         R30096-1
4           30096-41         R30096-2


Desired Output:

ID                ITEM               File1              File2
1              30092-61          R30092-1      R30092-2
2              30096-41          R30096-1      R30096-2
pauls681Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Dale FyeOwner, Developing Solutions LLCCommented:
with Access, the best way to do this would be something like:

TRANSFORM First(T3.File) AS FirstOfFile
SELECT T3.Item
FROM (SELECT T1.ID, T1.Item, T1.File, Count(T2.ID) as Rank
FROM yourTable as T1
LEFT JOIN yourTable as T2
ON T1.Item = T2.Item AND T2.File <= T1.File
GROUP BY T1.ID, T1.Item, T1.File
)  AS T3
GROUP BY T3.Item
PIVOT "File" & [Rank];

Open in new window


The subquery above will generate a Rank column can then be used in the column header in the crosstab query.

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
GrahamMandenoCommented:
The easiest (and probably fastest) option is to use a temporary table.

The temporary table can be built with an Item field and the appropriate number of FileN text fields.  (This number is the maximum of the number of original records for each item).

Simply start with an empty table and use a VBA loop to read the a Recordset on the original table, ordered by Item and File.  Each time the Item changes, save the previous record to the temp table (unless you are at the start) and add a new record to the temp table, storing the Item and resetting a counter to zero.
Then increment the counter and store the file from that record in rsTemp("File" & nCounter)
When you have read all the input records, save the last output record and close your Recordsets.

You can then export this temp table to Excel, or generate a report from it - whatever you want.

Note that your temporary table should never be in your main back-end where the data is stored, nor in your front-end containing your forms and code objects.  Instead, create it in a temporary database and add it to your front-end as a linked table.  Otherwise you will cause unnecessary database bloat and maximise the chance of corruption.

Good luck!
Graham Mandeno [Access MVP since 1996]
GrahamMandenoCommented:
Nice, Dale :-)
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Dale FyeOwner, Developing Solutions LLCCommented:
Thanks, Graham.  I've spent a lot of time over the years creating some complicated cross-tab queries for reports and Excel exports.  Too bad Access doesn't have a built-in Rank feature like SQL Server.
pauls681Author Commented:
Thank you for you help.
Dale... You are a genius!
I have it working, Thank you very much!
pauls681Author Commented:
Perfect solution!
Dale FyeOwner, Developing Solutions LLCCommented:
Glad to help.
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.