Results 1 to 6 of 6
  1. #1
    hycho is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2012
    Posts
    3

    Transposing Columns to Rows

    Hi All,



    I need to transpose some columns into rows, so that I can unique records in my database.

    Here's an example of data I have:

    ID Language
    1 English
    1 Spanish
    2 English
    2 French

    Here's the output I want (notice I now have two language columns instead of one):

    ID Language1 Language2
    1 English Spanish
    2 English French

    Does anyone how I might be able to write a VBA code for this?

    Thanks for your help.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,601
    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.

  3. #3
    hycho is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2012
    Posts
    3
    Hi,

    Thanks for the quick response. I tried using allen's code (see below), but when I compiled the code I get the compile error message "user-defined type not defined" for the line in red below (i.e. Dim rs As DAO.Recordset 'Related records). How do I resolve this issue? As a FYI, I have a similar code I use to run on my old computer, and it would run on my old computer. But when I got a new computer, the code doesn't work anymore. So I am thinking this might be an issue with my new computer missing some softwares/packets/etc.

    Thanks for your help.

    Here's the code I am refering to below:

    http://allenbrowne.com/func-concat.html

    Public Function ConcatRelated(strField As String, _
    strTable As String, _
    Optional strWhere As String, _
    Optional strOrderBy As String, _
    Optional strSeparator = ", ") As Variant
    On Error GoTo Err_Handler
    'Purpose: Generate a concatenated string of related records.
    'Return: String variant, or Null if no matches.
    'Arguments: strField = name of field to get results from and concatenate.
    ' strTable = name of a table or query.
    ' strWhere = WHERE clause to choose the right values.
    ' strOrderBy = ORDER BY clause, for sorting the values.
    ' strSeparator = characters to use between the concatenated values.
    'Notes: 1. Use square brackets around field/table names with spaces or odd characters.
    ' 2. strField can be a Multi-valued field (A2007 and later), but strOrderBy cannot.
    ' 3. Nulls are omitted, zero-length strings (ZLSs) are returned as ZLSs.
    ' 4. Returning more than 255 characters to a recordset triggers this Access bug:
    ' http://allenbrowne.com/bug-16.html
    Dim rs As DAO.Recordset 'Related records
    Dim rsMV As DAO.Recordset 'Multi-valued field recordset
    Dim strSql As String 'SQL statement
    Dim strOut As String 'Output string to concatenate to.
    Dim lngLen As Long 'Length of string.
    Dim bIsMultiValue As Boolean 'Flag if strField is a multi-valued field.

    'Initialize to Null
    ConcatRelated = Null

    'Build SQL string, and get the records.
    strSql = "SELECT " & strField & " FROM " & strTable
    If strWhere <> vbNullString Then
    strSql = strSql & " WHERE " & strWhere
    End If
    If strOrderBy <> vbNullString Then
    strSql = strSql & " ORDER BY " & strOrderBy
    End If
    Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)
    'Determine if the requested field is multi-valued (Type is above 100.)
    bIsMultiValue = (rs(0).Type > 100)

    'Loop through the matching records
    Do While Not rs.EOF
    If bIsMultiValue Then
    'For multi-valued field, loop through the values
    Set rsMV = rs(0).Value
    Do While Not rsMV.EOF
    If Not IsNull(rsMV(0)) Then
    strOut = strOut & rsMV(0) & strSeparator
    End If
    rsMV.MoveNext
    Loop
    Set rsMV = Nothing
    ElseIf Not IsNull(rs(0)) Then
    strOut = strOut & rs(0) & strSeparator
    End If
    rs.MoveNext
    Loop
    rs.Close

    'Return the string without the trailing separator.
    lngLen = Len(strOut) - Len(strSeparator)
    If lngLen > 0 Then
    ConcatRelated = Left(strOut, lngLen)
    End If

    Exit_Handler:
    'Clean up
    Set rsMV = Nothing
    Set rs = Nothing
    Exit Function

    Err_Handler:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "ConcatRelated()"
    Resume Exit_Handler
    End Function

  4. #4
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    India
    Posts
    616
    Check your references for Microsoft DAO 3.6 Library

  5. #5
    hycho is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2012
    Posts
    3
    I have checked my references to include Microsoft DAO 3.6 Library. The code now compiles fully. I will now have to figure out how to use the code. Thanks.

  6. #6
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    India
    Posts
    616
    Glad to know it worked !

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

Similar Threads

  1. Putting Rows in to columns
    By HowardlyDog in forum Queries
    Replies: 1
    Last Post: 06-29-2012, 01:39 PM
  2. "transposing vertical data into horizontal columns"
    By stevelondon in forum Queries
    Replies: 3
    Last Post: 03-20-2012, 12:36 PM
  3. Rows to columns
    By Jim.H. in forum Access
    Replies: 1
    Last Post: 01-28-2012, 05:29 PM
  4. Rows into Columns
    By chrisdavis in forum Programming
    Replies: 16
    Last Post: 12-22-2011, 01:58 PM
  5. Columns, Rows & Cells
    By Paul H in forum Reports
    Replies: 7
    Last Post: 09-21-2011, 12:27 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