Link to home
Create AccountLog in
Avatar of Ross
RossFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Adding carriage return to string

Using Allen Brownes (excellent) concat function:
Option Compare Database

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
    
    '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

Open in new window


I've tried everything I can think of to use a carriage return for the seperator, Chr(10), vbcrlf, etc but I'm stuck and don't seem to be able to!

Can someone help me modify the above (or pass the correct seperator to the above function) - I'd be forever grateful!

Thanks :)

Ross
Avatar of Kamaraj Subramanian
Kamaraj Subramanian
Flag of Singapore image

try with chr(13)
Avatar of Ross

ASKER

The problem is where to put that.

If I use it as the optional variable, it inserts those actual characters into the concatenated text. If I edit the function and add it after the strSeperator (IE: strOut = strOut & rs(0) & strSeparator & chr(13) it just ignores it.
try like

strOut = strOut & rsMV(0) & strSeparator & Chr(13) & Chr(10)

Open in new window


note : both Chr(13) & Chr(10) are used
Avatar of Ross

ASKER

nope - already tried that. It seems to ignore those completely.
try with vbNewLine
Hi Ross could you use the Replace function on the ConcatRelated function eg:
x=Replace(ConcatRelated(.....),",",vbCrLf)

Open in new window


Hope that helps
...Terry
Avatar of peter57r
Using a separator of

Chr(13) & chr(10)

with NO quotes - works here fine..

Example..
 txt1 = ConcatRelated("[Order Id]", "[Orders]", "[Customer id]=" & Me.[ID], , Chr(13) & Chr(10))
Don't fiddle with the code.
It works out of the box:

strConcat = ConcatRelated("YourFieldName", "YourTableName", , , vbCrLf)

/gustav
ASKER CERTIFIED SOLUTION
Avatar of Ross
Ross
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
No problem. That explains.

/gustav
Avatar of Ross

ASKER

My own investigation fixed the problem