Allen Browne's ConcatRelated

KevinBaker

New member
Local time
Today, 13:34
Joined
Aug 23, 2020
Messages
25
Is it possible add a new line between each value? For example, my current results look like this:

(555) 140-4726 [1] Home, (555) 331-6658 [2] Cell, (555) 345-7062 [3] *EMERG*

And I like to get this:
(555) 140-4726 [1] Home
(555) 331-6658 [2] Cell
(555) 345-7062 [3] *EMERG*

Thanks,
Kevin
 
In html, you would use <br>
In vba, you could use vbcr or vbcrlf
What are you using
 
In VBA, use vbCrLf instead of or along with comma separator.

In a query or textbox, use Chr(13) & Chr(10).
 
I'm using Allen Browne's ConcatRelated. This is the sql string.

Code:
SELECT tblMain.MnIdNum, tblMain.SortName, ConcatRelated("Phone","qryPhones","MainID_FK=" & [MnIdNum],""," | ") AS P
FROM tblMain;

I'm not sure where to actually use either vbCrLf or Chr(13) & Chr(10)
 
try this.
Code:
SELECT tblMain.MnIdNum, tblMain.SortName, ConcatRelated("Phone","qryPhones","MainID_FK=" & [MnIdNum],"",chr(13) & chr(10)) AS P
FROM tblMain;

If that does not work I would simply mod the function to hard wire it for returns

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:
    '               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
************************************************************************************************ 
  'Hard wire it here 
    strSeperator = vbcrlf
 

    '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
 
Last edited:
Code:
NewContent = Mid(Replace(Content, "(555)", vbCrLf & "(555)"), 3)
This is word processing.

In a database, it would be more productive to break it down into atomic content, i.e. create three data records here.
 
I'm using Allen Browne's ConcatRelated. This is the sql string.

Code:
SELECT tblMain.MnIdNum, tblMain.SortName, ConcatRelated("Phone","qryPhones","MainID_FK=" & [MnIdNum],""," | ") AS P
FROM tblMain;

I'm not sure where to actually use either vbCrLf or Chr(13) & Chr(10)
Perhaps as the Separator?
 
MajP - Your first recommendation worked. I pretty sure I tried that myself, but I must have had something wrong.

Thank you all for your help and insight.
 
I would add an optional string argument to the concat function to modify the separator.

So comma by default, but vbcrlf as an optional alternative, for example.
 
I would add an optional string argument to the concat function to modify the separator.

So comma by default, but vbcrlf as an optional alternative, for example.
Both methods have that already.?
 
^^^^
Oh yes, I see. I hadn't read every post in detail. Now I look closely I can see there is a strseparator in the code posted by @MajP
 

Users who are viewing this thread

Back
Top Bottom