What is the best way to do this in Access?
Create table tmp ( plant int, material vchar(20), workcenter int, setuptime vchar(20) ) insert into tmp values( 1, mat1, 2, 30) insert into tmp values( 1, mat1, 3, 30) insert into tmp values( 1, mat2, 3, 30) insert into tmp values( 1, mat2, 4, 30) insert into tmp values( 2, mat1, 4, 30) insert into tmp values( 2, mat1, 5, 30)
Result needs to look like.
Plant Material Workcenter1 Setuptime1 Workcenter2 Setuptime2 1 Mat1 2 30 3 30 1 Mat2 3 30 4 30 2 Mat1 4 30 5 30
I was thinking that this might work, but there has to be a better way.
SELECT t.Plant, t.Material, t.Workcenter as Workcenter1, t.setuptime as SetupTime1 t2.Workcenter as Workcenter2, t2.setuptime as SetupTime2 FROM tmp t LEFT JOIN tmp t2 on t.plant = t2.plant and t.material = t2.material
Thanks a bunch.
From what I understand of the problem you want to solve I don’t think it’s feasible using a standard SQL query (at least not in Access).
I tried to hack some code that does what you want (I think).
How to use it
Just copy/paste the code below in a VBA module.
From code, or, from the VBA IDE Immediate window if you want to test it, just call:
Assumptions
tempexists with the data you want to expand containing the plant/material/worksatation/etctemptable is not empty (I have omitted some code checks to avoid bloating the sample).result.Code
About the code
resulttable is entirely re-created every time you runExpandTable.WorkCenterXandSetupTimeXcolumns adapts to the actual number of unique Plant/Material pairs.Test database
You can download a test Access 2000 database from http://blog.nkadesign.com/wp-content/uploads/SO/SO547777.zip.
Anyway, hope it does what you want or at least gets your closer.