XL: Data Returned to Wrong Row Using SQLBind

ID: Q129156

The information in this article applies to:

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.

WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

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:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications

Additional query words: 5.0 5.0c 7.0 SQLBind SQLRetrieve blank
Keywords          : kbdta kbdtacode ODBCGen KbVBA 
Version           : WINDOWS:5.0,5.0c,7.0,7.0a,97; MACINTOSH:5.0,5.0a
Platform          : MACINTOSH WINDOWS
Issue type        : kbbug
Solution Type     : kbnofix

Last Reviewed: May 17, 1999