Link to home
Start Free TrialLog in
Avatar of MaxwellTurner
MaxwellTurnerFlag for Canada

asked on

Access 2007 - Insert Columns into rows

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
Avatar of PatHartman
PatHartman
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of MaxwellTurner

ASKER

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
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.
Thanks als!