MS Access报告中断字符?

时间:2011-04-07 15:57:36

标签: ms-access report

我在MS Access中构建了一个报告,其中包含一个包含网址的简短文本框。文本框的“CanGrow”选项设置为“是”。

因为此字段的报表上的水平空间有限,并且网址可能很长,而不是仅仅将网址溢出到文本长度强制的位置,如:

http://stackoverflow.com/que
stions/ask

我想知道是否有办法强制文本字符串在最后一个适当的字符处换行,在本例中为“/”字符。结果将是看起来更具可读性的东西,例如:

http://stackoverflow.com/
questions/ask

可以这样做吗?有关如何处理此任务的任何建议吗?

1 个答案:

答案 0 :(得分:2)

以下递归函数将根据用户定义的字符和最大行长度插入回车符/换行符。这对于固定宽度的字体最有效,但对于可变宽度字体也应该可以接受一些实验:

Function PrettyBreak(Txt As String, MaxCharsPerLine As Long, _
                     Optional BreakAfterChars As String = ":=-+&?./ ") As String
Dim t As String, i As Integer, Pos As Integer

    If Len(Txt) > MaxCharsPerLine Then
        t = Left(Txt, MaxCharsPerLine)
        For i = MaxCharsPerLine To 1 Step -1
            If InStr(BreakAfterChars, Mid(t, i, 1)) <> 0 Then
                Pos = i
                Exit For
            End If
        Next i
        If Pos = 0 Then
            PrettyBreak = t & vbCrLf & _
                          PrettyBreak(Mid(Txt, MaxCharsPerLine + 1), _
                                      MaxCharsPerLine, BreakAfterChars)
        Else
            PrettyBreak = Left(t, Pos) & vbCrLf & _
                          PrettyBreak(Mid(Txt, Pos + 1), _
                                      MaxCharsPerLine, BreakAfterChars)
        End If
    Else
        PrettyBreak = Txt
    End If
End Function

使用中:

?prettybreak("http://stackoverflow.com/questions/5583986/ms-access-report-line-break-at-character", 30)
http://stackoverflow.com/
questions/5583986/ms-access-
report-line-break-at-character