XL: SQLExecQuery and SQLRequest Limited to 255 CharactersLast reviewed: September 2, 1997Article ID: Q114992 |
The information in this article applies to:
SYMPTOMSIn Microsoft Excel, if you use the SQLExecQuery function or the SQLRequest function in a Visual Basic procedure to run a query on an external data source, and your query is larger than 255 characters, you may receive a general protection (GP) fault when you run the procedure.
CAUSEWhen you use a function in the XLODBC.XLA add-in, such as SQLRequest, and you use a query that contains more than 255 characters, the query is truncated after the first 255 characters. As a result, you can convert your query string into an array, where each element of the array can contain up to 255 characters; however, you may receive a GP fault if an element in the array is larger than 127 characters. Note that this behavior does not occur every time you run the procedure. The results are not consistent. You can also use the Range object to reference multiple cells that contain your query. Again, you may receive a GP fault if any of the cells in the range that contains your query contain more than 127 characters.
WORKAROUNDTo avoid receiving a GP fault, and to avoid having your query truncated when you use the SQLExecQuery or the SQLRequest functions with a query that is larger than 255 characters, use one of the following methods:
Array MethodLimit the elements in the array that you create to handle your query to 127 characters or less. This example uses the QueryToArray function to return your query as an array of values that each contain 127 characters of your query. This array can then be used as your "new" query.
' Dimension variables
Dim Shift As Integer, Size As Integer, I As Integer
' If no maximum string length is provided (MaxLength), then
' set maximum size of string in array to 127 characters
If IsMissing(MaxLength) Then MaxLength = 127
' Account for Option Base default value
Shift = LBound(Array(1)) - 1
' Determine the number of elements that will be contained
' in the array that this function returns by dividing the
' number of characters in the long query by the maximum
' size of the string
Size = (Len(Q) + MaxLength) / MaxLength
' Dimension array 'TmpArr'
ReDim tmparr(Size + Shift, 1 + Shift) As String
For I = 1 To Size
' Load array 'TmpArr' with one value for each 127 characters
' of your query
tmparr(I + Shift, 1 + Shift) = _
Mid$(Q, (I - 1) * MaxLength + 1, MaxLength)
Next I
' Assign array returned by QueryToArray function to temparr
QueryToArray = tmparr
End Function
Range MethodTo avoid truncating your query when you use the SQLExecQuery function or the SQLRequest function, you can also use the Range method to reference a query contained in multiple cells on a worksheet. The following is an example of using this method:
STATUSMicrosoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. This problem was corrected in Microsoft Excel for Windows version 5.0c and Microsoft Excel for Windows 95 version 7.0. Note that the 255 character limit when you use a function in the XLODBC.XLA add-in still exists, but you will no longer receive a GP fault.
MORE INFORMATIONYou can use the SQLRequest function to connect to an external data source and run a query from a worksheet, and then return the result as an array. The SQLExecQuery function executes a query on a data source with a connection that has been established using SQLOpen. Both of these functions are contained in the XLODBC.XLA add-in. You must establish a reference to the XLODBC.XLA file using the References dialog box before you can use either function in a procedure.
REFERENCESFor more information about the SQLExecQuery function, choose the Search button in the Visual Basic Reference and type:
SQLExecQuery functionFor more information about the SQLRequest function, choose the Search button in the Visual Basic Reference and type:
SQLRequest function |
Additional query words: 1.00 5.00 5.00c 7.00 msquery sql addins addin
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |