Solved

Access 2007 - Insert Columns into rows

Posted on 2016-09-16
6
47 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 36

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 36

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 36

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

726 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