Solved Splitting a Long String into Multiple Lines (1 Viewer)

Pac-Man

Active member
Local time
Tomorrow, 02:38
Joined
Apr 14, 2020
Messages
416
Here is the updated code that can hyphenate the words if the words are greater than certain length (passed as parameter):

Code:
' ----------------------------------------------------------------
' Procedure Name: SplitString
' Purpose: To restructure long lext into shorter strings of
' less or equal to some specified length keeping full words
' and punctuation.
' Parameter strToSplit (String): The long text string to be shortened
' Parameter intMaxLen (Integer): The max length of shortened string
' Author: Jack
' Date: 29-Nov-18
' Modified by: Pac-man
' Date: 09-May-23
' Modifications: Change to function, Added Hypheantion option, put optional output type to string/array, removed adding space during
'                the procedure which results of additional space at the end of each line due to which no of chr per line increased
'                causing moving new word into new line which could have been put in current line
' ----------------------------------------------------------------

Function SplitString(strToSplit As String, intMaxLen As Integer, Optional intMinWordLenForHyphen As Integer = 0, Optional intMinChrAfterHyphen As Integer = 0, Optional OutputResultAs As ssOutputAs = oaString) As Variant
    On Error GoTo Err_Handler

    'Parameters and Meaning
    'strToSplit As String - this is the long text field to whittle down
    'intMaxLen As Integer - this is the max line length of short comment
    'intMinWordLenForHyphen As Integer - Minimum word len to hyphenate (words less than this value will move to new line)
    'intMinChrAfterHyphen as Integer - Minimum no of character that are mandatory for hyphenated word to have in the line

    'Local Variables
    Dim varArray As Variant  'will hold individual words
    Dim varTemp() As Variant
    Dim itm As Variant
    Dim strTemp As String    ' the working string
    Dim intTemp As Integer   ' utility integer
 
    'Remove all "  " double spaces
    strTemp = strToSplit    ' the incoming string to split
    Do While InStr(strTemp, "  ") > 0
        strTemp = Replace(strTemp, "  ", " ")
    Loop
    
    'Remove all vbCrLf line breaks
    strTemp = strToSplit
    Do While InStr(strTemp, vbCrLf) > 0
        strTemp = Replace(strTemp, vbCrLf, " ")
    Loop

    'Split string to array
    varArray = Split(strTemp, " ")

    'Create strings of max or shorter length keeping full words
    strTemp = "": ReDim varTemp(1 To 1)
    For intTemp = 0 To UBound(varArray)
        If strTemp <> "" Then
            strTemp = strTemp & " " & varArray(intTemp)
        ElseIf strTemp = "" Then
            strTemp = varArray(intTemp)
        End If
        If intTemp = UBound(varArray) Then    'are we finished yet?
            varTemp(UBound(varTemp)) = strTemp
            strTemp = ""
            Exit For
        End If
        
        If Len(strTemp & varArray(intTemp + 1)) + 1 > intMaxLen Then
            'Hyphenate if hyphenateLen is greater than 0
            If (intMinWordLenForHyphen <= 0) Or (Len(varArray(intTemp + 1)) < intMinWordLenForHyphen) Then
StartNewLine:
                'Do Nothing and start new line
                intTemp = intTemp
                If IsEmpty(varTemp(UBound(varTemp))) Then
                    ReDim Preserve varTemp(1 To UBound(varTemp) + 1)
                    varTemp(UBound(varTemp) - 1) = strTemp
                    strTemp = ""
                End If
            Else
                'hyphenate the next words
                Dim hyphenatedWord As String
                
                hyphenatedWord = left(varArray(intTemp + 1), Len(varArray(intTemp + 1)) - (2 + Len(strTemp & varArray(intTemp + 1)) - (intMaxLen))) & "-" 'hyphenate the next word
                If (Len(hyphenatedWord) < intMinChrAfterHyphen + 1) Or (Trim(hyphenatedWord) = "-") Then GoTo StartNewLine
                
                'assign new value to next word by removing the characters added into current line
                varArray(intTemp + 1) = Mid(varArray(intTemp + 1), Len(hyphenatedWord))
                strTemp = strTemp & " " & hyphenatedWord
                GoTo StartNewLine
            End If
        Else
            intTemp = intTemp
        End If
    Next intTemp

    If OutputResultAs = oaArray Then
        SplitString = varTemp
        
    ElseIf OutputResultAs = oaString Then
        strTemp = vbNullString
        For intTemp = LBound(varTemp) To UBound(varTemp)
            If Len(strTemp) = 0 Then strTemp = varTemp(intTemp) Else strTemp = strTemp & vbCrLf & varTemp(intTemp)
        Next intTemp
        SplitString = strTemp
    End If

Exit_Handler:
    Exit Function

Err_Handler:
    Select Case Err.Number
        Case Else
            MsgBox "The following error has occured:" & vbCrLf & vbCrLf & _
                "Error Number: " & Err.Number & vbCrLf & _
                "Error Source: " & sModName & "\SplitString" & vbCrLf & _
                "Error Description: " & Err.Description & _
                Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl), _
                vbOKOnly + vbCritical, "An Error has Occured!"
            Resume Exit_Handler
    End Select
End Function
 

Josef P.

Well-known member
Local time
Today, 23:38
Joined
Feb 2, 2023
Messages
829
Noticed when looking over:
Code:
    'Remove all "  " double spaces
    strTemp = strToSplit    ' the incoming string to split
    Do While InStr(strTemp, "  ") > 0
        strTemp = Replace(strTemp, "  ", " ")
    Loop

    'Remove all vbCrLf line breaks
    strTemp = strToSplit  '<---- You do want to leave the double spaces in the text, don't you?  ;-)
    Do While InStr(strTemp, vbCrLf) > 0  '<---- No loop required
        strTemp = Replace(strTemp, vbCrLf, " ")  '<---- Double spaces might appear here again.
    Loop

Note: If the procedure is divided into several procedures, the programming with goto can be omitted.
 
Last edited:

jdraw

Super Moderator
Staff member
Local time
Today, 17:38
Joined
Jan 23, 2006
Messages
15,382
Pac-Man,

We are still guessing as to what the real issue is. It now appears you have a form and on that form you have a textbox. And you wish to take some long text string and break it into smaller strings such that the width (and length/depth) of the combined smaller strings fit within the bounds of said textbox/visible area. But we do not know the size of the textbox. We do not know the font you plan to use nor the font size.

Might be time to show us:
-the form and textbox
-the max length of long text
-the proposed font and size, and
-the intended use of the shorter strings in context.
 

Pac-Man

Active member
Local time
Tomorrow, 02:38
Joined
Apr 14, 2020
Messages
416
Pac-Man,

We are still guessing as to what the real issue is. It now appears you have a form and on that form you have a textbox. And you wish to take some long text string and break it into smaller strings such that the width (and length/depth) of the combined smaller strings fit within the bounds of said textbox/visible area. But we do not know the size of the textbox. We do not know the font you plan to use nor the font size.

Might be time to show us:
-the form and textbox
-the max length of long text
-the proposed font and size, and
-the intended use of the shorter strings in context.
We have a form in the office with fixed format/pattern (with predefined documented formatting like font style, size etc) in which have a field "Document Description" which is split in 3 lines. I want to create the same in access so I have to keep the same format. The code you provided fulfill my purpose that is why I marked the thread solved. I can split description text into multiple line and insert each line in respective text filed in the report.

I just wanted to add an extra option to hyphenate the next word if its length is 5 or 7 characters or more. Let's say first line of description is "The specifications for stainless steel for manufacturing of fixed beam assemblies and dynamic shock absorbers." And say our no characters limit reaches at "The specifications for stainless steel for manufactur" and when the whole wotd manufacturing is moved to the next line then it creates a gap (or a long white space at the end of the line) in the first line. It could be made better looking if it is like "The specifications for stainless steel for manufactu-" which filles the whole line. It is not mandatory but just make it good looking.

I hope it is now clear.
 

jdraw

Super Moderator
Staff member
Local time
Today, 17:38
Joined
Jan 23, 2006
Messages
15,382
Clearer, but seems a very dynamic requirement. If you change the length for shortened line, you may get a more pleasant/acceptable display.
I don't see a pattern that would apply generally, but I'd be interested if someone sees one.

Update:
I reviewed your code to do the hyphenation. Modified the logic to get hyphenation in your code to produce the output below. Also renamed the function to SplitStringADJ (adjusted splitstring)

Code:
Sub testLongStr() ' USING MY TEST ROUTINE TO USE pacmAN'S FUNCTION
          Dim varShortComment As Variant, i As Integer
          Dim mytest As String  'jibberish to make a long string
10    mytest = "The specifications for stainless steel for manufacturing of fixed beam assemblies and dynamic shock absorbers."
20        varShortComment = SplitStringADJ(mytest, 27, 5)
          'create the short comments
30        For i = 1 To UBound(varShortComment)
40            Debug.Print "cmt" & i & "  " & Len(varShortComment(i)) & "  " & varShortComment(i)
50        Next i
60        Debug.Print
End Sub

RESULT:
Code:
cmt1  22  The specifications for
cmt2  27  stainless steel for manufa-
cmt3  27  cturing of fixed beam asse-
cmt4  24  mblies and dynamic shock
cmt5  10  absorbers.

Using SplitStringADJ(mytest, 39, 2)
Result:
Code:
cmt1  38  The specifications for stainless steel
cmt2  39  for manufacturing of fixed beam assemb-
cmt3  33  lies and dynamic shock absorbers.

Using SplitStringADJ(mytest, 23, 3)
Result:
Code:
cmt1  22  The specifications for
cmt2  23  stainless steel for ma-
cmt3  20  nufacturing of fixed
cmt4  23  beam assemblies and dy-
cmt5  22  namic shock absorbers.

Also: I didn't realize your code had been assisted by Chat GPT --I just saw the variable names>
 
Last edited:

Mike Krailo

Well-known member
Local time
Today, 17:38
Joined
Mar 28, 2020
Messages
1,044
Optional OutputResultAs As ssOutputAs = oaString
Just tried the above SplitString function and it does not compile due to the oaString type. Any idea what this user defined type is?
 

jdraw

Super Moderator
Staff member
Local time
Today, 17:38
Joined
Jan 23, 2006
Messages
15,382
Mike,
I have no idea of his oaString and oaArray. I basically ignored his "solution" in #21. Like you, I saw there were things that were not defined.
I did play with the original requirements and did get working code. It was created a few weeks after my last post in the thread. I found that getting the requirements clearly organized was a main issue in getting a working solution.

I'm attaching a database with the code involved to meet the requirement. You'll note that it's all in 1 module. There are some test lines (For/Next) commented out where you can test various desired line lengths.

If you do find an answer to the oaString/oaArray, I 'd like to see the result.
 

Attachments

  • HyphenCopy.accdb
    428 KB · Views: 69

Mike Krailo

Well-known member
Local time
Today, 17:38
Joined
Mar 28, 2020
Messages
1,044
I'm guessing it was a custom user defined type that he was using. Thanks for uploading your take on it. Should be interesting to see how it works with different fonts and sizes on a form. I just just had a peek at it and it seems to work pretty good.
 

Pac-Man

Active member
Local time
Tomorrow, 02:38
Joined
Apr 14, 2020
Messages
416
Hello,

Sorry for late response. I'm away from the pc on which the code is present so I'm writing it down here. It is enum for getting output as array of lines of text after splitting or as a string with each line separated by vbCRLf.

Code:
Public Enum ssOutputAs
    oaString = 1
    oaArray = 2
End Enum

Best Regards
 

jdraw

Super Moderator
Staff member
Local time
Today, 17:38
Joined
Jan 23, 2006
Messages
15,382
Pac-Man,

Can you post a copy of your latest working database - or apart of it that shows the code for string manipulation?
 

Pac-Man

Active member
Local time
Tomorrow, 02:38
Joined
Apr 14, 2020
Messages
416
Pac-Man,

Can you post a copy of your latest working database - or apart of it that shows the code for string manipulation?
Hi,

Please find attached truncated database showing how string is split in the report. This is what I am using currently in my project.

Best Regards
 

Attachments

  • Meetings Records.accdb
    512 KB · Views: 54

Pac-Man

Active member
Local time
Tomorrow, 02:38
Joined
Apr 14, 2020
Messages
416
@jdraw, there were few issues in my code due to which skipped use of hyphen but the db that you provided in post#27 is working perfectly so I replaced code in my project with the one in your uploaded db (post#27) and now started using hyphen too as required. Thanks a lot.
 

jdraw

Super Moderator
Staff member
Local time
Today, 17:38
Joined
Jan 23, 2006
Messages
15,382
Pac-Man,

Glad you found the database from #27 useful. As I mentioned to Mike, I found that clearly understanding the requirement took some time. I noted some differences in your database example in #32 and the requirement as I understood it (eventually), but since you had a working solution, I didn't comment further. Hopefully, the #27 database meets the requirement. I did try it with various long text strings and a variety of lengths before and after the comma and desired line lengths.
Again, happy to help.
 

Users who are viewing this thread

Top Bottom