Solved

Access 2007 - Insert Columns into rows

Posted on 2016-09-16
6
44 Views
Last Modified: 2016-09-16
I have a field with a comma separated list.

Custno      Itemno             Breakdown
CPC000       CPPMONO1    60,0,40
AOV001       CPPMONO1    75,0,25
CASAER       CPPMONO1    75,0,25
ENDSIL       CPPMONO1    75,0,25

I have managed to break it apart into separate columns using a module I created:

Custno      GS      OEM      Ser      Itemno
CPC000       60        0              40      CPPMONO1
AOV001       75        0              25      CPPMONO1
CASAER       75        0              25      CPPMONO1
ENDSIL       75        0              25      CPPMONO1

HERE IS WHERE I AM STUCK
I need to create 3 rows instead of 3 fields:

Custno      Per      Type      Itemno
CPC000      60      GS              CPPMONO1
CPC000      0      OEM      CPPMONO1
CPC000      40      SER              CPPMONO1
AOV001      75      GS              CPPMONO1
AOV001      0      OEM      CPPMONO1
AOV001      25      SER              CPPMONO1
CASAER      75      GS              CPPMONO1
CASAER      0      OEM      CPPMONO1
CASAER      25      SER              CPPMONO1
ENDSIL      75      GS              CPPMONO1
ENDSIL      0      OEM      CPPMONO1
ENDSIL      25      SER              CPPMONO1

I have tried every type of TRANSFORM/PIVOT query I can think of without success.  I'm sure I'm missing something dumb ... anyone know how I can do this?

Max
0
Comment
Question by:MaxwellTurner
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:PatHartman
ID: 41802146
Modify the code you have that creates the three columns to create rows instead.  If you need help with the modification, please post the code you are currently using.
0
 
LVL 40

Accepted Solution

by:
als315 earned 500 total points
ID: 41802153
You can use Union query. Create query, select type "Union" and paste this text to window:
Select CustNo, GS as Per, "GS" as Type, ItemNo From Table1
union select  CustNo, OEM as Per, "OEM" as Type, ItemNo From Table1
union select  CustNo, Ser as Per, "SER" as Type, ItemNo From Table1

Open in new window

Replace Table1 with real table name
DBUnion.accdb
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 41802233
I didn't suggest the union query because it sounded like you wanted a permanent conversion.  If you are going to run your code to convert the MVF to separate columns, you might as well modify that procedure and write the data out as rows so you will have what you want.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 1

Author Comment

by:MaxwellTurner
ID: 41802264
Okay I feel a little silly ... was looking in the wrong direction totally.  The Union works great!

@Pat:  I copied some VBA from some website designed for a list of words and used it on my numbers.  My VBA is too weak to create a procedure to create the rows, but if you can help me, I'm all ears.  

This is the code I used to create the columns.  I create a query, with colums - GS: GetCSWord(Breakdown,1), OEM: GetCSWord(Breakdown,2), SER: GetCSWord(Breakdown,3).  I doubt it can be modified to create rows ... but I'm no expert:

Option Explicit

Function CountCSWords(ByVal S) As Integer
' Counts the words in a string that are separated by commas.

Dim WC As Integer, Pos As Integer
   If VarType(S) <> 8 Or Len(S) = 0 Then
     CountCSWords = 0
     Exit Function
   End If
   WC = 1
   Pos = InStr(S, ",")
   Do While Pos > 0
     WC = WC + 1
     Pos = InStr(Pos + 1, S, ",")
   Loop
   CountCSWords = WC
End Function

Function GetCSWord(ByVal S, Indx As Integer)
' Returns the nth word in a specific field.

Dim WC As Integer, Count As Integer, SPos As Integer, EPos As Integer
   WC = CountCSWords(S)
   If Indx < 1 Or Indx > WC Then
     GetCSWord = Null
     Exit Function
   End If
   Count = 1
   SPos = 1
   For Count = 2 To Indx
     SPos = InStr(SPos, S, ",") + 1
   Next Count
   EPos = InStr(SPos, S, ",") - 1
   If EPos <= 0 Then EPos = Len(S)
   GetCSWord = Trim(Mid(S, SPos, EPos - SPos + 1))
End Function

Open in new window


Max
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 41802270
It could be modified but if the Union works for you, then use that.  I was just trying to make the process more efficient so you'd end up with a table that has what you needed.
0
 
LVL 1

Author Closing Comment

by:MaxwellTurner
ID: 41802301
Thanks als!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

861 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