Solved

Access 2007 - Insert Columns into rows

Posted on 2016-09-16
6
53 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 38

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 38

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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 38

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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

635 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