?
Solved

One to Many Relationship, Concatenate data

Posted on 2005-04-25
5
Medium Priority
?
737 Views
Last Modified: 2008-02-01
Hello.

I am having some trouble concatenating data from the table on the many side of a one to many relationship.

I have two tables:

tblEmpInfo
[EmpID] text, primary key (this is field filled with numbers, but is not used for any mathematical computation)
[FirstName] text
[LastName] text
{other fields that aren't used for this issue}

tblEmpDepend
[DepID] AutoNumber, primary key
[EmpID] text
[DepType]
[DepName]

the two tables are used to show each employee's dependents.  Let me give an example:

tblEmpInfo
0001 | John | Doe
0002 | Joe | Stump

tblEmpDepend
1 | 0001 | Spouse | Jane
2 | 0001 | Child | Jim
3 | 0001 | Child | Shana
4 | 0002 | Spouse | Bess
5 | 0002 | Child | Bob

What I'd like to end up with is a query that will concatenate the [DepName] field on the many side of the table into a string.  Like so:

0001 | John | Doe | Jane, Jim, Shana
0002 | Joe | Stump | Bess, Bob.

I tried using the following code in a module, but when I called the function in this Query:
SELECT TblEmpInfo.*, fConcatFld("TblEmpDepend","EmpID","DepName","String",[EmpID]) AS DepNames
FROM TblEmpInfo;
I kept getting the "type mismatch" # 13 error

this is the code for the function (which I got from http://www.mvps.org/access/modules/mdl0004.htm )

'************ Code Start **********
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Function fConcatChild(strChildTable As String, _
                    strIDName As String, _
                    strFldConcat As String, _
                    strIDType As String, _
                    varIDvalue As Variant) _
                    As String
'Returns a field from the Many table of a 1:M relationship
'in a semi-colon separated format.
'
'Usage Examples:
'   ?fConcatChild("Order Details", "OrderID", "Quantity", _
                "Long", 10255)
'Where  Order Details = Many side table
'       OrderID       = Primary Key of One side table
'       Quantity      = Field name to concatenate
'       Long          = DataType of Primary Key of One Side Table
'       10255         = Value on which return concatenated Quantity
'
Dim db As Database
Dim rs As Recordset
Dim varConcat As Variant
Dim strCriteria As String, strSQL As String
    On Error GoTo Err_fConcatChild
   
    varConcat = Null
    Set db = CurrentDb
    strSQL = "Select [" & strFldConcat & "] From [" & strChildTable & "]"
    strSQL = strSQL & " Where "
   
    Select Case strIDType
        Case "String":
            strSQL = strSQL & "[" & strIDName & "] = '" & varIDvalue & "'"
        Case "Long", "Integer", "Double":    'AutoNumber is Type Long
            strSQL = strSQL & "[" & strIDName & "] = " & varIDvalue
        Case Else
            GoTo Err_fConcatChild
    End Select
   
    Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)

    'Are we sure that 'sub' records exist
    With rs
        If .RecordCount <> 0 Then
            'start concatenating records
            Do While Not rs.EOF
                varConcat = varConcat & rs(strFldConcat) & ";"
                .MoveNext
            Loop
        End If
    End With
       
    'That's it... you should have a concatenated string now
    'Just Trim the trailing ;
    fConcatChild = Left(varConcat, Len(varConcat) - 1)
       
Exit_fConcatChild:
    Set rs = Nothing: Set db = Nothing
    Exit Function
Err_fConcatChild:
    Resume Exit_fConcatChild
End Function

'************ Code End **********

Does anyone have any ideas as to what could be giving me the type mismatch error?  If so, any ideas as to how I could fix it?

Any help is greatly appreciated, as always.

0
Comment
Question by:wolfeman76
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Stephen_Perrett
ID: 13862606
The only problem I can see is the name of the function you are calling should be

fConcatChild

and not

fConcatFld

your query should be as follows:

SELECT TblEmpInfo.*, fConcatChild("TblEmpDepend","EmpID","DepName","String",[EmpID]) AS DepNames
FROM TblEmpInfo

But the error message suggests that one of your fields may not have the datatype you think it has.

Steve
0
 
LVL 11

Expert Comment

by:Stephen_Perrett
ID: 13862623
Check that the datatype of EmpID really is text

Steve
0
 
LVL 1

Author Comment

by:wolfeman76
ID: 13866767
Um. Yeah.

I found my mistake (aside from calling the wrong function)

This database uses elements of ADO so the recordset needed to be declared a DAO recordset
so instead of:
 Dim rs As Recordset

It should have been
 Dim rs As DAO.Recordset

stupid computers doing what they're told...Geez.

Steve, Thanks for your help
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 14456964
PAQed with points refunded (250)

modulo
Community Support Moderator
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question