Results 1 to 3 of 3
  1. #1
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218

    Concatenate Union Query to remove duplicate records

    I'm having some issues with a union query which contains duplicate records. The SQL for the query is below:



    Code:
    SELECT tblCustomer.OrganizationFK, tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblCustomer.LastName, tblCustomer.FirstName, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK, tblBuilding.BuildingName, tblRooms.RoomName, tblSecOptions.SecName, tblCabinet.CabinetName, tblEquipment.CabinetFK, tblEquipment.EquipmentNameFK, tblEquipment.EquipmentBrandFK, tblEquipment.EquipmentNetworkTypeFK, tblEquipment.EquipmentIP, tblEquipment.SerialNumFROM tblSecOptions INNER JOIN (((tblBuilding INNER JOIN (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK) INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN (tblCabinet LEFT JOIN tblEquipment ON tblCabinet.CabinetPK = tblEquipment.CabinetFK) ON tblRooms.RoomsPK = tblCabinet.RoomsFK) ON tblSecOptions.SecOptionPK = tblRooms.SecOptionFK
    UNION SELECT tblCustomer.OrganizationFK, tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblCustomer.LastName, tblCustomer.FirstName, tblRooms.BuildingFK, tblRooms.RoomsPK, tblBuilding.BuildingName, tblRooms.RoomName, tblSecOptions.SecName, tblCabinet.CabinetName, tblEquipment.CabinetFK, tblEquipment.EquipmentNameFK, tblEquipment.EquipmentBrandFK, tblEquipment.EquipmentNetworkTypeFK, tblEquipment.EquipmentIP, tblEquipment.SerialNum
    FROM tblSecOptions INNER JOIN (((tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) LEFT JOIN (tblCabinet LEFT JOIN tblEquipment ON tblCabinet.CabinetPK = tblEquipment.CabinetFK) ON tblRooms.RoomsPK = tblCabinet.RoomsFK) INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK) ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK) ON tblSecOptions.SecOptionPK = tblRooms.SecOptionFK;
    I need to make the Building ID and the Room ID (tblFacilityMgr.BuildingFK, tblBuilding.BuildingPK, tblRoomsPOC.RoomsFK, tblRooms.RoomsPK) unique and concatenate all other related data, to remove duplicates.

    Below is the code I found online for a concatenate function. I cannot seem to figure out how to apply it to my query. It has something to do with "ConcatRelated("strField", "strTable")" Also I don't believe that will work to concatenate all the fields. it looks like it only handles one. Thank you so much for your help.

    Code:
    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:
        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

    Edit: after more searching, I found this: http://allenbrowne.com/func-concat.html

    It does explain a lot and I'll definitely play around with it tomorrow, but I'm not sure how I'll implement it in such a large union query. It looks simple for just a couple fields.

  2. #2
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    using union queries is often a sign of an 'unnormalised' db. You may get more responses if you explain in English what your data consists of and what you are trying to achieve

  3. #3
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218
    I was wrong to use a union query. Instead, I needed the IN() function.

    I created a new form which contained only BuildingPK and RoomsPK and then created a search form which used a filter variable and set it to tblBuildings.BuildingPK IN( sub query).

    Its working now.

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

Similar Threads

  1. Replies: 3
    Last Post: 03-22-2014, 04:32 PM
  2. Remove Duplicate Entries in Query Design
    By samanthaM in forum Access
    Replies: 1
    Last Post: 10-27-2013, 11:26 AM
  3. Suming Duplicate Records in Union Query
    By Sqnwk in forum Access
    Replies: 1
    Last Post: 10-30-2012, 06:10 PM
  4. Query to concatenate records - newb
    By bentod in forum Queries
    Replies: 7
    Last Post: 10-08-2012, 12:06 PM
  5. Concatenate records in Query
    By cleon in forum Queries
    Replies: 3
    Last Post: 04-08-2012, 11:14 AM

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