Data Returned to Wrong Row Using SQLBind (129156)
The information in this article applies to:
- Microsoft Excel 97 for Windows
- Microsoft Excel for Windows 95
- Microsoft Excel for Windows 5.0
This article was previously published under Q129156 SYMPTOMS
When you use the SQLRetrieve function to return data to Microsoft Excel,
the data that will be returned to a cell is one row below the cell
specified in a previous call to the SQLBind function.
CAUSE
It is possible to bind one column of an external data source to a specific
cell location on a worksheet. This is accomplished using the SQLBind
function.
The SQLRetrieve function performs the action of retrieving the results of a
query to a worksheet. There is an option associated with this function that
allows you to retrieve data with or without column headings.
When you retrieve data using SQLRetrieve, if you do not request column
headings to be returned, and you have previously used the SQLBind function
to designate the location for data to be returned, the cell location
designated will be left blank, and your data will be returned beginning one
row below the specified location.
WORKAROUNDMicrosoft 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.
When you use SQLBind and SQLRetrieve together, as in the "Symptoms" and
"Cause" sections above; you will need to adjust for the blank cell when you
retrieve data that does not contain column headings.
One way to work around this problem is to delete the blank cell after the
data is returned, shifting the remainder of the column upward. The
following sample Microsoft Visual Basic for Applications macro (Sub
procedure) accomplishes this workaround.
Sample Visual Basic Procedure
Sub FixBindProblem()
' Open ODBC channel.
chan = SQLOpen("DSN=NWind")
' Send SQL query to channel.
SQLExecQuery chan, "SELECT * FROM customer"
' Bind third column of result set to ActiveCell.
SQLBind chan, column:=3, reference:=ActiveCell
' Retrive query results. Default setting is do not retrieve column
' headings. No destination is specified, so data will be returned
' to bound location.
SQLRetrieve chan
' Close ODBC channel.
SQLClose chan
' Delete blank cell, shifting remainder of column upwards.
ActiveCell.Delete Shift:=xlUp
End Sub
Note that another method for adjusting the blank cell is to adjust the
reference made in the SQLBind command to be one row above the originally
planned cell. However, this method can present a problem if you
want to return data to row number one, because there is no row above row
number one.
STATUS
Microsoft has confirmed this to be a problem in the versions of Microsoft
Excel listed at the beginning of this article.
MORE INFORMATION
If the SQLBind function has not been used to specify a location for the
data to be returned, the SQLRetrieve function will return the data and/or
column headings to the correct destination, as specified by the SQLRetrieve
function.
REFERENCES
For additional information about getting help with Visual Basic for
Applications, please see the following article in the Microsoft Knowledge
Base:
163435 VBA: Programming Resources for Visual Basic for Applications
Modification Type: | Minor | Last Reviewed: | 10/11/2006 |
---|
Keywords: | kbbug kbdtacode kbnofix KB129156 |
---|
|