Concatenation and maximum string length in VBA, access
I had string constraints issues in access-vba.
The point is that access (sometimes) limits the length of a string to about 255 characters.
However, depending on how the string was constructed, it can grow to 255 characters.
Here is a sample code WORK:
Dim strReq as String
strReq = "SELECT exampleField1, exampleField2, exampleField3, exampleField4, exampleField5 "
strReq = strRec & ", exampleField6, exampleField7, exampleField8, .... [etc. insert many fields, you get it]"
strReq = strReq & " FROM myTable INNER JOIN Tbl2 ON ...[many JOINs as well]"
And so on, I often work with large queries, so 256 characters are easily swapped out.
However, these examples don't work:
Dim strReq as String
strReq = "SELECT exampleField1, exampleField2, exampleField3, exampleField4, exampleField5 " & _
", exampleField6, exampleField7, exampleField8, .... [etc. insert many fields, you get it]" & _
" WHERE exampleField1 = x AND exampleField2 = y AND exampleField3 = z" & _
" ORDER BY 1,2,3,4,5,6"
And that doesn't work either:
Dim strReq as String
Dim strWhere as String
strReq = "SELECT exampleField1, exampleField2, exampleField3, exampleField4, exampleField5 "
strReq = strRec & ", exampleField6, exampleField7, exampleField8, .... [etc. insert many fields, you get it]"
strWhere = "WHERE exampleField1 = x "
strWhere = strWhere & "AND exampleField2 = y"
strWhere= strWhere & " AND exampleField3 = z"
strReq = strReq & strWhere [& strJoin / strOrder / strHaving / etc]
I know I know how I may or may not be able to concatenate strings, but I would like to know how strings exactly work with vba access , because, I agree, it seems pretty random so far ...
* (Note that these lines are assumed to be longer than 255 characters, and the request is just an example, syntax errors or exact length are not relevant here)
* Edit - adding the code I'm using (with a working version, tried both listening versions to clean up the code and both were listening
strReq = "SELECT " & IIf(Len(rsRap.Fields("top")) > 0, " TOP " & rsRap.Fields("top"), "") & " " & rsRap.Fields("champs") & ", Sum([Canada]*[QuantitΓ©]) AS Montant, Sum(TblDetailCom.QuantitΓ©) AS Qty " & IIf(Len(rsRap.Fields("rep")) > 0, ", NickName", "")
strReq = strReq & " FROM (SELECT * FROM TblRepresentant WHERE RefRep not In(13,15,26,27,28)) AS TblRepresentant INNER JOIN "
strReq = strReq & " ((TblProduits LEFT JOIN TblTypBijoux ON TblProduits.Type = TblTypBijoux.IdTypBijoux) "
strReq = strReq & " INNER JOIN (TblCouleur INNER JOIN ((TblClients INNER JOIN ((TblComm LEFT JOIN RqMaxIdTrait ON TblComm.ID = RqMaxIdTrait.IdCommande) "
strReq = strReq & " LEFT JOIN TblTraitement ON RqMaxIdTrait.MaxOfIdTrait = TblTraitement.IdTrait) ON TblClients.ID = TblComm.RefClient) "
strReq = strReq & " INNER JOIN TblDetailCom ON TblComm.ID = TblDetailCom.RefCom) ON TblCouleur.ID = TblDetailCom.RefCoul) "
strReq = strReq & " ON TblProduits.IdMod = TblDetailCom.RefProd) ON TblRepresentant.RefRep = TblClients.RefRepre "
strReq = strReq & " WHERE (TblClients.RefRepre <> 5 OR (TblClients.RefRepre=5 AND TblClients.ID In (1210,219,189,578))) "
'(((TblProduits.Coll)=16) AND((TblComm.CoDatCom)>=#2011-01-01# And (TblComm.CoDatCom)<=#2014-01-01#) " 'Params Collection (16) DteDeb/fin
'strReq = strReq & " AND "
If Len(rsRap.Fields("type")) > 0 Then
strReq = strReq & " AND TblProduits.[Type] = " & rsRap.Fields("type")
End If
If Len(txtDe) > 0 Then
strReq = strReq & " AND TblTraitement.DtTrait >= #" & txtDe & "# "
End If
If Len(txtA) > 0 Then
strReq = strReq & " AND TblTraitement.DtTrait <= #" & txtA & "# "
End If
If Len(rsRap.Fields("pays")) > 0 Then
strReq = strReq & " AND TblClients.ClPaiePays = '" & rsRap.Fields("pays") & "' "
End If
If Len(rsRap.Fields("rep")) > 0 Then
strReq = strReq & " AND TblClients.RefRepre = " & rsRap.Fields("rep")
End If
If Len(rsRap.Fields("col")) > 0 Then
strReq = strReq & " AND TblProduits.Coll=" & rsRap.Fields("col")
End If
If Len(rsRap.Fields("group")) > 0 Then
strReq = strReq & " GROUP BY " & rsRap.Fields("group") & IIf(Len(rsRap.Fields("rep")) > 0, ", NickName", "")
End If
strReq = strReq & " HAVING Sum([Canada]*[QuantitΓ©]) >= 0 "
If Len(rsRap.Fields("order")) > 0 Then
strReq = strReq & " ORDER BY " & rsRap.Fields("order")
End If
source to share
You seem to agree that a VBA string can be more than 255 characters long. 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 are facing a problem based on the method you use to concatenate strings. I don't know where exactly this problem is, but I can say that there is a limit to the number of lines you can use when adding to a line. For example, the following code compiles and runs without error. However, if I add another line continuation ( & cstrSegment _
), the compiler complains "Too many line continuation".
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 this describes the problem you are seeing, the limit is based on line continuation, not line length. If necessary, you can work around this limitation by building the string in several steps. Do "MyBigString = MyBigString & cstrSegment _"
up to the line continuation limit, then add in MyBigString
with another block "MyBigString = MyBigString & cstrSegment _"
.
Make sure you are not wrong about how many characters you see. Perhaps the situation is that you only see the first 255 characters, but in fact the line contains much more. This would make sense since you reported that you were not getting an error when building the string, which apparently fails.
Confirm the actual string length with Len()
:
Debug.Print "Len(MyBigString): " & Len(MyBigString)
You can also print the content of the line in the Immediate window to see what it contains:
Debug.Print MyBigString
You can use Ctrl+ gto open the Immediate window.
source to share