XL5: SQLRetrieve/SQLRequest Converts Text Values to Numeric

ID: Q118941


The information in this article applies to:


SYMPTOMS

In Microsoft Excel for Windows, when you use the SQLRetrieve function or the SQLRequest function in a Visual Basic procedure, some of the values that are represented as text in your table may be returned to your worksheet as numbers.

For example, you may have a column of data in a table with the following values:


   000123
     0567
    00001 
When you use SQLRetrieve or SQLRequest to return this data to a worksheet, the values may be returned as the following:

      123
      567
        1 
That is, the leading zeros are dropped. SQLRetrieve and SQLRequest convert these text values to their numeric equivalents. Likewise, text values such as "1e1" and "1/2" will be converted to their numeric equivalents "1.00E+01" and "Jan-2" respectively.


WORKAROUND

To have the SQLRetrieve function or the SQLRequest function return text values as text, you must modify the SQL statement so that the field is returned to the worksheet as a formula.

Below is an example using the SQLRetrieve function that will return the text values in the Itemnum field as numbers.

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Sample Visual Basic Procedure


Sub GetData()
Dim Chan As Variant
Dim NumRows, NumCols as Integer

  'Make connection and assign channel number to that connection
  Chan = SQLOpen("DSN=NWind")

  'Execute SQL Query on specified channel
  NumCols = SQLExecQuery (Chan, _
     "Select Itemnum from c:\excel5\test.dbf")

  'Return the data from the specified channel to the worksheet
  NumRows = SQLRetrieve (Chan, ActiveCell)

  'Close the connection to the specified channel
  SQLClose (Chan)
End Sub 
To have SQLRetrieve return these text values in Itemnum as text, modify the SQLExecQuery statement as follows:


   NumCols = SQLExecQuery (Chan, _
     "Select '=""' + ItemNum + '""' from c:\excel5\test.dbf") 
Once the data has been returned to the worksheet, you can take an additional step to remove the formulas in the cells but leave the values in the cells. The modified procedure would look like the following:


Sub GetData()
Dim Chan As Variant
Dim NumRows, NumCols as Integer

  'Make connection and assign channel number to that connection
  Chan = SQLOpen("DSN=NWind")

  'Execute SQL Query on specified channel
  NumCols = SQLExecQuery (Chan, _
     "Select '=""' + ItemNum + '""' from c:\excel5\test.dbf")

  'Return the data from the specified channel to the worksheet
  NumRows = SQLRetrieve (Chan, ActiveCell)

  'Close the connection to the specified channel
  SQLClose (Chan)

  'Remove formulas but retain values of returned data
  Activecell.Resize(NumRows, NumCols).Copy
  Activecell.Resize(NumRows,NumCols).PasteSpecial xlValues
  Application.CutCopyMode = False

End Sub 


STATUS

Microsoft 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.

Additional query words: 1.00 scientific notation exponent


Keywords          : xlquery 
Version           : 5.00
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: July 15, 1999