Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:40:14+00:00 2026-05-11T06:40:14+00:00

What is the best way to do this in Access? Create table tmp (

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:40:15+00:00Added an answer on May 11, 2026 at 6:40 am

    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:

    ExpandTable 

    Assumptions

    • the table temp exists with the data you want to expand containing the plant/material/worksatation/etc
    • the temp table is not empty (I have omitted some code checks to avoid bloating the sample).
    • the expanded table will be created in a new table called result.

    Code

    Public Sub ExpandTable()      Dim db As DAO.Database     Dim rs As DAO.Recordset, rs2 As DAO.Recordset     Dim td As DAO.TableDef     Dim fd As DAO.Field     Dim maxWorkCenters As Integer     Dim i As Integer     Dim sql As String      Set db = CurrentDb      ' Delete the old result table if there was one '     On Error Resume Next     db.TableDefs.Delete 'result'     On Error GoTo 0      ' Create the result table '     Set td = db.CreateTableDef('result')     td.Fields.Append td.CreateField('Plant', dbInteger)     td.Fields.Append td.CreateField('Material', dbText)     ' Get the maximum number of workcenters we will need '     ' for a given Plan/Material combination '     sql = 'SELECT Count(*) FROM Temp GROUP BY Plant, Material'     Set rs = db.OpenRecordset(sql, dbOpenSnapshot)     maxWorkCenters = Nz(rs.Fields(0).Value, 0)     rs.Close     Set rs = Nothing     ' Create as many columns as we need to fit all these combinations '     For i = 1 To maxWorkCenters         td.Fields.Append td.CreateField('WorkCenter' & i, dbText)         td.Fields.Append td.CreateField('SetupTime' & i, dbInteger)     Next i     db.TableDefs.Append td      ' Now get the data into the new table '     Dim lastPlant As Variant, lastMaterial As Variant     Dim curcol As Integer     sql = 'SELECT Plant, Material, Workcenter, Setuptime FROM Temp ORDER BY Plant, Material, WorkCenter'     Set rs = db.OpenRecordset(sql, dbOpenSnapshot)     Set rs2 = db.OpenRecordset('result', dbOpenDynaset)     With rs         lastPlant = 0         lastMaterial = ''         Do While Not .EOF             If (Nz(!Plant) <> lastPlant) Or (Nz(!Material) <> lastMaterial) Then                 If rs2.EditMode = dbEditAdd Then                     ' Save the previously edited record if any '                     rs2.Update                 End If                  ' Different plant/material, so we add a new result '                 rs2.AddNew                 rs2!Plant = !Plant                 rs2!Material = !Material                 rs2!WorkCenter1 = !WorkCenter                 rs2!SetupTime1 = !Setuptime                 lastPlant = Nz(!Plant)                 lastMaterial = Nz(!Material)                 curcol = 1             Else                 ' Same plant/material combi, so we fill the next column set '                 curcol = curcol + 1                 rs2.Fields('Workcenter' & curcol).Value = !WorkCenter                 rs2.Fields('SetupTime' & curcol).Value = !Setuptime             End If             .MoveNext         Loop         If rs2.EditMode = dbEditAdd Then             ' Save the last result '             rs2.Update         End If     End With      Set rs2 = Nothing     Set rs = Nothing     Set db = Nothing  End Sub 

    About the code

    • The result table is entirely re-created every time you run ExpandTable.
    • The number of additional WorkCenterX and SetupTimeX columns adapts to the actual number of unique Plant/Material pairs.
    • You’ll have to modify code to suit your exact needs.
    • You’ll also have to clean it up a bit as some things could probably be expressed a bit better but you get the jest.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 157k
  • Answers 157k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If I have a java class which is package-private (declared… May 12, 2026 at 11:03 am
  • Editorial Team
    Editorial Team added an answer First, wrap your head around this programming assignment. Believe me,… May 12, 2026 at 11:03 am
  • Editorial Team
    Editorial Team added an answer If you can use LINQ, LINQ to XML is an… May 12, 2026 at 11:03 am

Related Questions

Possible Duplicate: Is there a way for MS Access to grab the current Active
I have been asked to setup a course leaflet system for a college. For
I am trying to implement the simplest shared 'files' folder for a website but
I have an access database on a windows machine, which I must import into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.