Create the following function in a regular module:
Public Function BreakTextAtX(varOriginal As Variant, _
Optional strBreakCharacter As String = " ", _
Optional lngMaxLength As Long = 72) As Variant
' Code written by Ken Snell -- 15 November 2008
' strOriginal is the original text string
' strBreakCharacter is the character that is used to break the
' text into separate lines (e.g., a blank space)
' lngMaxLength is the maximum length for each separate line
Dim strNewString As String, strWorking As String, strPart As String
Dim strOriginalNoCrLf As String
Dim lngPosition As Long, lngHold As Long, lngLength As Long
Dim lngWorkLength As Long
lngLength = Len(varOriginal & "")
If lngLength > 0 Then
strOriginalNoCrLf = Replace(Replace(CStr(varOriginal), vbCr, ""), vbLf,
"")
strNewString = ""
lngPosition = 1
Do While lngPosition <= lngLength
strWorking = Mid(strOriginalNoCrLf, lngPosition, lngMaxLength)
lngWorkLength = Len(strWorking)
If lngWorkLength < lngMaxLength Then
If Len(strNewString) > 0 And Len(strWorking) > 0 Then _
strNewString = strNewString & vbCrLf
strNewString = strNewString & strWorking
Exit Do
Else
lngHold = InStrRev(strWorking, strBreakCharacter)
If lngHold = 0 Then lngHold = lngWorkLength
If Len(strNewString) > 0 Then _
strNewString = strNewString & vbCrLf
strNewString = strNewString & Left(strWorking, lngHold)
lngPosition = lngPosition + lngHold
End If
Loop
BreakTextAtX = strNewString
Else
If IsNull(varOriginal) = True Then
BreakTextAtX = varOriginal
Else
BreakTextAtX = ""
End If
End If
End Function
Then use this code for the form's CommandButton's Click event:
Private Sub CommandButtonName_Click()
Dim strTextWithoutCrLf As String
Const strBreakCharacter As String = " "
Const lngMaxLength As Long = 70
Me.MemoTextboxName.Value = _
BreakTextAtX(Me.MemoTextboxName.Value, _
strBreakCharacter, lngMaxLength)
End Sub
--
Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/