Results 1 to 4 of 4
  1. #1
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126

    Sort of transposing a table ????

    I have a table with records looking likge this:



    Click image for larger version. 

Name:	BF.jpg 
Views:	11 
Size:	38.3 KB 
ID:	32050

    How can I change it to look like this:

    BarnID ForælderID1 ForælderID2 ForælderID 3 .... (max 5)
    26 52 53 107
    27 69 70
    28 26 27
    29 28 31

    ect.

    Using VBA or SQL in VBA code ?

    There will be from NONE to 5 ID's pr. BarnID.

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    This may get you started.
    I created a table tblBarns (I added MyID as PK)

    barnid ForID myiD
    26 52 1
    26 53 2
    26 107 3
    27 69 4
    27 70 5
    28 26 6
    28 27 7
    29 28 8
    29 31 9


    Code:
    Sub ksor1()
        Dim db As DAO.Database
        Dim rs1 As DAO.Recordset, rs2 As DAO.Recordset
        Dim sqlF As String
        Dim sqlB As String
        Dim sOut As String
    10  sqlB = "Select distinct BarnID from tblBarns"
    20  sqlF = "Select ForID from tblBarns where BarnID ="
    
    30  Set db = CurrentDb
    40  Set rs1 = db.OpenRecordset(sqlB)
    50  Do While Not rs1.EOF
    60      sOut = rs1!Barnid & " "
    70      Set rs2 = db.OpenRecordset(sqlF & rs1!Barnid)
    80      Do While Not rs2.EOF
    90          sOut = sOut & rs2!ForID & " "
    100         rs2.MoveNext
    110     Loop
    120     Debug.Print sOut
    130     rs1.MoveNext
    140 Loop
    End Sub
    Result:
    Code:
    26 52 53 107 
    27 69 70 
    28 26 27 
    29 28 31

  3. #3
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126
    THX ... I'll try to get some ideas from it !

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Also try:

    TRANSFORM First(tblBarns.ForID) AS FirstOfForID
    SELECT tblBarns.barnID
    FROM tblBarns
    GROUP BY tblBarns.barnID
    PIVOT DCount("*","tblBarns","barnID=" & [barnID] & " AND myID<" & [myID])+1;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 08-02-2017, 06:01 PM
  2. Transposing Linked Data
    By Rustin788 in forum Access
    Replies: 4
    Last Post: 03-09-2017, 03:46 PM
  3. transposing data in a report
    By brianrunsphilly in forum Reports
    Replies: 3
    Last Post: 10-23-2013, 02:22 PM
  4. Transposing Columns to Rows
    By hycho in forum Programming
    Replies: 5
    Last Post: 07-19-2012, 01:17 AM
  5. Transposing Data
    By NoiCe in forum Queries
    Replies: 0
    Last Post: 11-09-2008, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums