I don't think you can change the behaviour of the standard textbox. You
may be able to find another textbox control that works the way you want.
Alternatively, you could use a calculated field in the query to insert
spaces or hyphens or newlines at regular intervals to allow wrapping.
Using the rgxReplace() function I've pasted at the end of this message,
this calculated field will insert a space every 80 characters:
S: rgxReplace([Sequence],"(.{80})","$1 ",False,True)
I have a text box on a report that can contain some very long strings I pull
out of the database. The text box spans nearly the entire width of the
report and I want the string to format like a paragraph. The problem is
that the strings sometimes contain long chunks of data with no spaces and
occasional chunks with spaces. The text box seems to interpret these as
"words" and returns to the next line whenever one of these long "words"
won't fit on the line thereby leaving a lot of white space at the end of
some lines. Typical word wrap behavior. I want the text to behave more
like a paragraph that gets hyphenated, i.e., go to the end of the line each
time before beginning the next line. I don't care if hyphens get added to
the end of lines or not.
Public Function rgxReplace(Optional Target As Variant, _
Optional Pattern As String = "", _
Optional Replacement As String = "", _
Optional CaseSensitive As Boolean = False, _
Optional ReplaceAll As Boolean = True, _
Optional Multiline As Boolean = False, _
Optional FailOnError As Boolean = True, _
Optional Persist As Boolean = True) _
As Variant
'Regular expression replacement function suitable for use
'in VBA generally and in Access queries.
'Takes a search string (Target), a regular expression
'(Pattern), and a replacement string (Replacement).
'If a substring of Target matches Pattern, that substring
'is replaced by Replacement. (The substring can be the whole
'of Target.)
'CaseSensitive matches regardless of case
'but does not affect the case of Replacement.
'ReplaceAll controls whether all substrings
'that match Pattern are replaced or only the first.
'Multiline controls whether a Target containing
'newlines is parsed as one multiline string or a
'succession of one-line strings.
'Persist controls whether the compiled RegExp object
'remains in memory ready for the next call to the
'function or whether it is disposed of immediately. This
'means the function can be used in queries without having
'to create, compile, use and destroy
'a new RegExp object for each row being processed.
'But it also means that the object remains in memory
'after the query has run. To destroy the object and release
'the memory, call this function one last time with no arguments.
'
'Calling the function with different arguments (e.g. a new
'Pattern and/or Replacement) recompiles the RegExp object, so
'the function can be used in different queries. However there
'may be problems if two threads are calling the function at
'the same time.
Const rgxPROC_NAME = "rgxReplace"
Static oRE As Object
'Static declaration means we don't have to create
'and compile the RegExp object every time the function
'is called.
On Error GoTo ErrHandler
rgxReplace = Null 'Default return value
'NB: if FailOnError is false, returns Null on error
If IsMissing(Target) Then
'This is the signal to dispose of oRE
Set oRE = Nothing
Exit Function 'with default value
End If
'Create the RegExp object if necessary
If oRE Is Nothing Then
Set oRE = CreateObject("VBScript.Regexp")
End If
With oRE
'Check whether the current arguments (other than Target)
'are different from those stored in oRE, and update them
'(thereby recompiling the regex) only if necessary.
If CaseSensitive = .IgnoreCase Then
.IgnoreCase = Not .IgnoreCase
End If
If ReplaceAll <> .Global Then
.Global = ReplaceAll
End If
If Multiline <> .Multiline Then
.Multiline = Multiline
End If
If Pattern <> .Pattern Then
.Pattern = Pattern
End If
'Finally, do the replacement
If IsNull(Target) Then
rgxReplace = Null
Else
rgxReplace = .Replace(CStr(Target), Replacement)
End If
End With
'Tidy up and normal exit
If Not Persist Then Set oRE = Nothing
Exit Function
ErrHandler:
If FailOnError Then
With Err
Select Case .Number
'Replace the default "object-defined error" message
Case 13: .Description = "Type mismatch, probably because " _
& "the ""Target"" argument could not be converted to a string"
Case 5017: .Description = "Syntax error in regular expression"
Case 5018: .Description = "Unexpected quantifier in regular
expression"
Case 5019: .Description = "Expected ']' in regular expression"
Case 5020: .Description = "Expected ')' in regular expression"
Case Else
If oRE Is Nothing Then 'Failed to create Regexp object
.Description = "Could not create VBScript.RegExp object. " &
Err.Description
Else 'Unexpected error
.Description = rgxPROC_NAME & ": " & .Description
End If
End Select
Set oRE = Nothing
.Raise Err.Number, rgxPROC_NAME, _
rgxPROC_NAME & "(): " & .Description
End With
Else 'Fail silently
Err.Clear
Set oRE = Nothing
End If
End Function