3

When doing a simple select * query to an excel workbook, it cuts off text after 255 characters. Are recordset fields limited to 255 characters? if not, how can I get the full field text from the recordset?

img1 The test line should have over 1400 characters All other line items are less than 255 characters, and are correct.

Sub ExportCallLogs()

Dim conn As Object
Dim rs As Object
Dim dbpath As String

' Create the connection string.
dbpath = "mypathhere.xlsx"

' Create the Connection and Recordset objects.
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

' Open the connection and execute.
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbpath & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
conn.Open sConnString
Set rs = conn.Execute("SELECT * FROM [Sheet1$]")

' Check we have data.
If rs.EOF Then
    MsgBox "Error: No records returned.", vbCritical
    Exit Sub
End If

'print headers and recordset
Workbooks.Add
For i = 0 To rs.Fields.Count - 1
    Cells(1, i + 1).Value = rs.Fields(i).Name
Next
Range("A2").CopyFromRecordset rs

Columns.AutoFit
Columns(i).ColumnWidth = 55

' Clean up
rs.Close
conn.Close
Set conn = Nothing
Set rs = Nothing

End Sub
7
  • Could you upload sample workbook to some cloud storage? Commented Dec 21, 2018 at 19:39
  • I found that the fields are limited to 255 characters, and updated my question. Let me know if you still need an example WB. Commented Dec 21, 2018 at 19:42
  • If you think there's a limit, then why text is cut off with variable length? Please, upload workbook. Commented Dec 21, 2018 at 19:48
  • I'm not sure how to upload a workbook. Just joined and it looks like others have said it's against the rules, and I don't see an option. Commented Dec 21, 2018 at 20:15
  • You can upload to Google Drive or OneDrive or DropBox - and attach a link. Commented Dec 21, 2018 at 20:27

2 Answers 2

3

Found the very weird problem, and solution. This ended up working for exporting data through a query and inserting data.

This is an issue with the Jet OLEDB provider. It looks at the first 8 rows of the spreadsheet to determine the data type in each column. If the column does not contain a field value over 256 characters in the first 8 rows, then it assumes the data type is text, which has a character limit of 256. The following KB article has more information on this issue: http://support.microsoft.com/kb/281517

Sign up to request clarification or add additional context in comments.

2 Comments

I don't see the URL that you shared as an actual working link, but everything you stated sounds totally correct.
I added empty spaces so that it doesn't look weird to end user :-)
0

You can often work around this by changing the format of the Excel column from Text to General.

2 Comments

Its currently set to General
This is not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.