Solved

Access 2007 - Insert Columns into rows

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

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 39

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 34

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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 34

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now