Create a concatenated list of child table values from an Access database without using VBA

后端 未结 1 814
孤城傲影
孤城傲影 2020-12-22 10:43

I have an Access 2010 database with a relationship between parent and child tables. I would like to be able to query the database from an external application and show value

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-22 11:07

    Issue

    Historically, the Access solution for a GROUP_CONCAT()-type query has been to use a VBA function like Allen Browne's ConcatRelated() (ref: here). However, custom VBA functions are only available to queries run from within Microsoft Access itself, so this is not a viable solution for queries against an Access database from some other application (e.g., a .NET application using OLEDB or ODBC).

    Solution

    With an Access 2010 (or newer) database we can emulate the behaviour of a MySQL GROUP_CONCAT() query by adding a Long Text ("Memo") field to the parent table and using data macros on the child table to maintain the concatenated list.

    For example, for tables [Parents] ...

    ParentID  ParentName   
    --------  -------------
           1  Homer Simpson
           2  Ned Flanders 
    

    ... and [Children] ...

    ChildID  ParentID  ChildName          DisplayOrder
    -------  --------  -----------------  ------------
          1         1  Lisa                          2
          2         1  Bart                          1
          3         2  Rod, the elder                1
          4         1  Maggie                        3
          5         2  Todd, the younger             2
    

    ... we can add a new Memo/Long Text field named [ChildList] to the [Parents] table and then add the following data macros to the [Children] table:

    [Named Macro: UpdateChildList]

    UpdateChildList.png

    
    
        
            
                
            
            
                
                    newList
                    Null
                
                
                    
                        
                            
                                
                            
                            
                                
                            
                            
                                
                            
                        
                        [c].[ParentID]=[prmParentID] And [c].[ChildName] Is Not Null
                    
                    
                        
                            
                                Not IsNull([newList])
                                
                                    
                                        newList
                                        [newList] & ";" & Chr(160)
                                    
                                
                            
                        
                        
                            newList
                            [newList] & [c].[ChildName]
                        
                    
                
                
                    
                        Parents
                        [Parents].[ParentID]=[prmParentID]
                    
                    
                        
                            
                            
                                
                                    Parents.ChildList
                                    [newList]
                                
                            
                        
                    
                
            
        
    
    

    [After Insert]

    AfterInsert.png

    
    
        
            
                
                    Children.UpdateChildList
                    
                        
                    
                
            
        
    
    

    [After Update]

    AfterUpdate.png

    
    
        
            
                
                    
                        Updated("ParentID") Or Updated("ChildName")
                        
                            
                                Children.UpdateChildList
                                
                                    
                                
                            
                            
                                
                                    Updated("ParentID")
                                    
                                        
                                            Children.UpdateChildList
                                            
                                                
                                            
                                        
                                    
                                
                            
                        
                    
                
            
        
    
    

    [After Delete]

    AfterDelete.png

    
    
        
            
                
                    Children.UpdateChildList
                    
                        
                    
                
            
        
    
    

    Results

    As changes are made to the child table the list in the parent table will automatically be updated:

    ParentID  ParentName     ChildList                        
    --------  -------------  ---------------------------------
           1  Homer Simpson  Bart; Lisa; Maggie               
           2  Ned Flanders   Rod, the elder; Todd, the younger
    

    Notes

    1. The [ChildList] field is for display purposes only. Editing the values in that field will not change the values in the child table.

    2. The list is separated with ";" & Chr(160) to differentiate it from any ";" & Chr(32) pairs that may be in the actual data. If the non-breaking space (Chr(160)) characters mess up wrapping of the list then we could use the Replace() function in our query to convert ";" & Chr(160) to ";" & Chr(32) or "," & Chr(32) or whatever would be most appropriate.

    3. To populate the lists with existing child data we simply need to "update" one child record for each parent, like so

    UPDATE Children SET ChildName=ChildName 
    WHERE ChildID IN (SELECT MIN(ChildID) AS m FROM Children GROUP BY ParentID)
    

    0 讨论(0)
提交回复
热议问题