Results 1 to 3 of 3
  1. #1
    emunson is offline Novice
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    1

    Using a query to move data to new columns

    Hi,

    I have an access database with multiple tables that I need to export into a single spreadsheet. The tables are set up with one-to-many relationships between them; for example, in my Collection table, I have a song ID and the title of the song (each song only appears once in this table). In my Composers table, I have the song ID along with a composer's name. In the Composers table, songs can have multiple composers, in which case a given song will appear in multiple records, each time with a different composer's name.

    So when I run a query that appends the names from the Composers table to the songs from the Collection table, I end up with each song having an extra record for each additional composer. Incidentally, I don't know whether there is any limit to the number of composers a song may have.

    Is there a way to write a query that can put each individual composer in a separate field, but keep them all in the same record? I need all of the information for each song to appear on a single line.

    So, this is what I'm getting now:

    CollectionID | Title | Composer
    20 | No one will ever know my heart | Foree, Mel
    20 | No one will ever know my heart | Rose, Fred
    21
    | Pal of my cradle days | Piantadosi, Al
    22 | Perfect song | Breil, Joseph C.
    23 | Say mister have you met Rosie's sister | Harrison, Charles, 1883-1955
    23 | Say mister have you met Rosie's sister | Rose, Fred
    24 | Three little words | Ruby, Harry

    And this is what I need:

    CollectionID | Title | Composer1 | Composer2
    20 | No one will ever know my heart | Foree, Mel | Rose, Fred
    21 | Pal of my cradle days | Piantadosi, Al


    22 | Perfect song | Breil, Joseph C.
    23 | Say mister have you met Rosie's sister | Harrison, Charles, 1883-1955 | Rose, Fred

    If this isn't possible with a query, could you recommend another approach?


    Thanks,
    Liz

  2. #2
    NoellaG's Avatar
    NoellaG is offline VIP
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    Hi,

    using VBA you can concatenate all composers for 1 songs in a string, and then fill a temp table with the values you want.

    gr
    NG

  3. #3
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    I have two tables
    tblSong: ID(AutoNumber PK), SongName (Text)
    tblComposer: Link_ID(Number), NameofComposer (Text)

    The table tblComposer is linked to tblSong one-many. So one song can have more than 1 composer.

    I have attached a sample DB check query5 which shows the name of the composers in a Single row separated by ","

    I have used the following function:

    Option Compare Database
    Dim intMyID As Integer
    Dim strSQL As String
    Dim strComposerString As String
    Function StrComposer(intMyID) As String
    strComposerString = ""
    strSQL = "Select * From tblComposer Where [Link_ID] =" & intMyID
    Set rst = CurrentDb.OpenRecordset(strSQL)
    If rst.EOF And rst.BOF Then
    Exit Function
    End If
    Do While Not rst.EOF
    If Len(strComposerString) > 0 Then
    strComposerString = strComposerString & ", " & rst!NameofComposer
    Else
    strComposerString = rst!NameofComposer
    End If
    rst.MoveNext
    Loop
    Set rst = Nothing
    StrComposer = strComposerString
    End Function


    This function basically does what NoellaG has so wonderfully explained.


    Refer to the attached db.
    Check Module2 for the function
    Query5 to Check the Result

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

Similar Threads

  1. Replies: 1
    Last Post: 08-09-2010, 03:59 PM
  2. Sorting date columns in query
    By madcreek in forum Queries
    Replies: 3
    Last Post: 06-02-2010, 07:45 AM
  3. Replies: 3
    Last Post: 02-08-2010, 09:00 AM
  4. Move data from one table to another
    By rebyrd in forum Queries
    Replies: 2
    Last Post: 12-24-2009, 12:52 AM
  5. Add Columns to query
    By 4petessake in forum Access
    Replies: 0
    Last Post: 06-15-2007, 01:38 PM

Tags for this Thread

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