SYMPTOMS
In Microsoft Excel, you can use the Xlodbc.xla add-in macro functions to
return data from an external source to your worksheet. The XLODBC macro
functions SQLRetrieve and SQLRequest may not behave as expected if you
attempt to return more than 248 columns of data. If the SQLExecQuery
function is used to create a query, this function will correctly return the
number of columns in the data set resulting from the query even if this
number exceeds 248.
In Microsoft Excel 5.0 for Windows, when you attempt to use SQLRetrieve or
SQLRequest to return more than 248 columns of data, you may receive the
error message:
Excel caused a General Protection Fault in module XLODBC.DLL at
0001:52E3
In Microsoft Excel 5.0 for Windows NT, Microsoft Excel 5.0 for the
Macintosh, Microsoft Excel 7.0 for Windows 95 and Microsoft Excel 97, if
you use SQLRetrieve or SQLRequest to return more than 248 columns of data,
you will receive only the first 248 columns of the result set but you will
not receive an error.
WORKAROUND
To work around this problem, use any of the following methods.
Method 1: Using Xlodbc.xla
You cannot return more than 248 columns of data to Microsoft
Excel using the Xlodbc.xla macro functions unless you execute
two separate queries--one query to return the first 248
columns and another query to return the remaining columns.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
Method 2: Using Xlquery.xla
You can use the Microsoft Query add-in to return more than
248 columns. The macro below demonstrates an example.
Sub GetData()
Dim ConStr As String
Dim SQL As String
'Open the Microsoft Query Add-in
Workbooks.Open Application.LibraryPath & _
"\msquery\xlquery.xla"
'Define the Connection String and the SQL Query
ConStr = "DSN=NWind"
SQL = "Select * from c:\windows\msapps\msquery\test.dbf"
'Execute the query and return the data to cell A1 on
'Sheet1--
'Include the field names and do not store the query on
'the worksheet
Run "QueryGetData", ConStr, SQL, False, True, False, _
Sheets("Sheet1").Range("A1"), True
End Sub
Method 3: Using Data Access Objects (DAO) in Microsoft Excel 7.0 and 97
You can use DAO to return more than 248 columns. The macro
below demonstrates an example. To use DAO in a Microsoft
Excel 7.0 macro, click References on the Tools menu while the
module sheet is active and select Microsoft DAO 3.0 Object
Library.
To use DAO in a Microsoft Excel 97 macro, click References on
the Tools menu in Visual Basic Editor and select Microsoft
DAO 3.5 Object Library.
Sub GetData()
Dim db as Database
Dim rs as Recordset
'Open the dBASE database in the specified directory
Set db = OpenDatabase("c:\windows\msapps\msquery", False,_
False, "dBASE IV;")
'Create a recordset that contains all of the records in
'the table Test.dbf
Set rs = db.OpenRecordset("Select * from Test")
'Copy the data in the recordset to Sheet1!A2
Sheets("Sheet1").Range("A2").CopyFromRecordset rs
'Return the field names in the recordset to row 1
For I = 0 to rs.Fields.Count -1
Sheets("Sheet1").Cells(1,I +1) = rs.Fields(i).Name
Next
End Sub