Dear experts,
I have multiple records associated with the same person (name_id is a unique key per person). I need to create a list with one row per person and all records concatenated in that row.
PersonA Record1
PersonA Record2
PersonB Record3
PersonB Record4
PersonC Record5
into
PersonA Record1; Record2
PersonB Record3; Record4
PersonC Record5
I tried to use a UDF to return a concatenated list of sub-record values that I found on the web. With my limited understanding I plugged my variables into the query with the UDF but it returns initial multiple rows per person and blank second column where I would expect to have my combined records.
What am I doing wrong here?
Thanks a lot for any feedback!
UDF
I have multiple records associated with the same person (name_id is a unique key per person). I need to create a list with one row per person and all records concatenated in that row.
PersonA Record1
PersonA Record2
PersonB Record3
PersonB Record4
PersonC Record5
into
PersonA Record1; Record2
PersonB Record3; Record4
PersonC Record5
I tried to use a UDF to return a concatenated list of sub-record values that I found on the web. With my limited understanding I plugged my variables into the query with the UDF but it returns initial multiple rows per person and blank second column where I would expect to have my combined records.
What am I doing wrong here?
Thanks a lot for any feedback!
Code:
SELECT [name_id], fConcatChild("TableName","name_id","Records","String",10255) AS SubFormValues
FROM [TableName];
UDF
Code:
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
'
' Set a reference to DAO
Dim db As DAO.Database
Dim rs As DAO.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:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set db = Nothing
Exit Function
Err_fConcatChild:
Resume Exit_fConcatChild
End Function