Vertical Rows to Horizontal Columns

ChristineMD

Board Regular
Joined
Jul 29, 2008
Messages
53
I have a query that generates items and their category codes.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
For these items there could be up to 7 unique category codes, but all will likely NOT have all 7<o:p></o:p>
So I have a table that has:<o:p></o:p>
Item CatCode<o:p></o:p>
Item1 12345<o:p></o:p>
Item1 12346<o:p></o:p>
Item2 12341 <o:p></o:p>
Item2 12342<o:p></o:p>
Item2 12346<o:p></o:p>
Item4 12344<o:p></o:p>
<o:p></o:p>
I want to get this data into a horizontal format with one record per ITEM, with its up to 7 categories in separate columns<o:p></o:p>
Item Cat1 Cat2 Cat3 (etc-->)
Item1 12345 12346<o:p></o:p>
Item2 12341 12342 12346<o:p></o:p>
Item4 12344<o:p></o:p>
<o:p></o:p>
I've taken a stab at modifying a piece of code I have to see if I can get it to work for this purpose. Can this work? I think I'm missing some pieces about defining an array possibly. It's the if loop and using the loop counter to define and populate the variable I'm not sure of. You guys and gals are always so helpful. I appreciate the look in advance!


Code:
Public Function outletcats() As Boolean
    On Error Resume Next
 
    Dim db As DAO.Database, rst As DAO.Recordset, sSQL As String
    Dim strdisplayitem As String, strcat1 As String, strcat2 As String, strcat3 As String, strcat4 As String, strcat5 As String, strcat6 As String, strcat7 As String
 
    cnt = 1
 
    Set db = CurrentDb()
    'Call RecreateTables(db)
    sSQL = "DELETE FROM tOutletCatsFinal"
    db.Execute sSQL
 
    sSQL = "SELECT displayitem, CatCode FROM tOutletItems2  ORDER BY displayitem ASC"
    Set rst = db.OpenRecordset(sSQL, dbOpenSnapshot)
 
    If Not rst.BOF And Not rst.EOF Then
      rst.MoveFirst
      strdisplayitem = rst!displayitem
      strcat(cnt) = rst!CatCode
 
 
      rst.MoveNext
      Do Until rst.EOF
        If strdisplayitem = rst!displayitem Then
          strcat(cnt) = rst!CatCode
 
        Else
          sSQL = "INSERT INTO tOutletCatFinal (displayitem, Cat1, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7) VALUES('" & strdisplayitem & "','" & strcat1 & "','" & strcat2 & "','" & strcat3 & strcat2 & "','" & strcat4 & "','" & strcat5 & "','" & strcat6 & "','" & strcat7 & "','" & "')"
          db.Execute sSQL
            strdisplayitem = rst!displayitem
            strcat(cnt) = rst!CatCode
 
      End If
        rst.MoveNext
        cnt = cnt + 1
      Loop
 
      ' Insert Last Record
      sSQL = "INSERT INTO tOutletCatFinal (displayitem, Cat1, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7) VALUES('" & strdisplayitem & "','" & strcat1 & "','" & strcat2 & "','" & strcat3 & strcat2 & "','" & strcat4 & "','" & strcat5 & "','" & strcat6 & "','" & strcat7 & "','" & "')"
      db.Execute sSQL
    End If
 
    Set rst = Nothing
    Set db = Nothing
End Function
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi, you can do this using the next SQL statement.
I tried to use your field and table names from the code you posted, but I'm not sure if all names are correct.

Code:
TRANSFORM First(CatCode) AS NewCatCode
SELECT DisplayItem
FROM tOutletItems2
GROUP BY DisplayItem
PIVOT CatCode
 
Upvote 0
Thanks to both for your replies, never even got a chance to come back here til now - will check both out - thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,267
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top