concatenation and max length of string in VBA, access

后端 未结 2 1797
南笙
南笙 2021-01-16 14:54

I\'ve had severas problems with strings in access-vba.

The thing is, access (sometimes) limit the string\'s length to about 255 characters.

However, dependin

相关标签:
2条回答
  • 2021-01-16 15:14

    You seem to accept the fact that a VBA string can contain more than 255 characters. As an example this code creates a 264 character string.

    Const cstrSegment As String = "0123456789" & vbCrLf
    Dim MyBigString As String
    Dim i As Long
    For i = 1 To 22
        MyBigString = MyBigString & cstrSegment
    Next
    Debug.Print "Len(MyBigString): " & Len(MyBigString)
    

    Rather you're encountering trouble based on the method you use to concatenate strings. I don't know where that trouble is exactly, but I can tell you there is a limit to the number of line continuations you can use when adding to a string. For example the following code compiles and runs without error. However if I add one more line continuation (& cstrSegment _), the compiler complains "Too many line continuations".

    MyBigString = MyBigString & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment _
        & cstrSegment
    

    If that describes the problem you're seeing, the limitation is based on line continuations, not string length. If needed, you could work around that limit by building the string in multiple steps. Do "MyBigString = MyBigString & cstrSegment _" up to the limit of line continuations, then add to MyBigString with another "MyBigString = MyBigString & cstrSegment _" block.

    Make sure you're not misled by how many character you see. Perhaps the situation is you're only seeing the first 255 characters, but the string actually contains many more. That would make sense since you reported you're not getting an error building the string apparently fails.

    Confirm the actual length of the string with Len():

    Debug.Print "Len(MyBigString): " & Len(MyBigString)
    

    You can also print the string's content to the Immediate window to see what it contains:

    Debug.Print MyBigString
    

    You can use Ctrl+g to open the Immediate window.

    0 讨论(0)
  • 2021-01-16 15:22

    When concatenating strings for SQL, add a vbCrLf character when lines might grow long. Access seems to have trouble ingesting VBA strings (to execute as SQL) greater than about 1000 characters. e.g.

    strSQL = strSQL & "SELECT some fields " & vbcrlf & "FROM some table "
    
    0 讨论(0)
提交回复
热议问题