XL: SQLRetrieve Defines Field Names on Active Workbook

ID: Q117644


The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a


SUMMARY

In Microsoft Excel, if you are using the Visual Basic, Applications Edition SQLRetrieve command, and you want each column of results to be declared as a named range on the worksheet, you can set the "NamedRngLogical" argument to TRUE.

The name of each range is the field name of a column's result set. The syntax for the SQLRetrieve statement is as follows:


   SQLRetrieve(ConnectionNum, DestinationRef, MaxColumns, MaxRows,
     ColNamesLogical, RowNumsLogical, NamedRngLogical,
     FetchFirstLogical) 
When you execute the SQLRetrieve command with the argument "NamedRngLogical" set to TRUE, the column names in the result set will always be defined on the active workbook regardless of what the "DestinationRef" argument is set to. If no workbooks are active when you run the SQLRetrieve command, and if "NamedRngLogical" is set to TRUE, you will receive the #N/A error value; however, the records will still be retrieved to the range specified by destination.

NOTE: The information in this article also applies to the Microsoft Excel 4.0 command SQL.EXEC.QUERY.


MORE INFORMATION

Visual Basic Code Example

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/overview/overview.asp
Note that a line that is preceded by an apostrophe introduces a comment in the code--comments are provided to explain what the code is doing at a particular point in the procedure. Note also that an underscore character (_) indicates that code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line continuation character. For more information about Visual Basic for Applications programming style, see the "Programming Style in This Manual" section in the "Document Conventions" section of the "Visual Basic User's Guide."

The following sample Visual Basic code uses the CUSTOMER.DBF file included with the sample data source NWind and assumes that:

  1. The NWind ODBC driver is installed and points to the Windows or a shared MSAPPS\MSQUERY directory.


  2. The module sheet has references to XLODBC.XLA found in the EXCEL\LIBRARY\MSQUERY directory, under the TOOLS-REFERENCES menu.


To run the example, follow these steps:

  1. In a new module sheet, enter in the following code:
    
          Sub GetData()
    
             ' To define the ranges for the column names in the workbook to 
             ' which the data is returned, use the following macro:
    
             Dim DestRange As Object
             Dim Chan, NumCols, NumRows As Variant
             Dim i As Integer
    
             Set DestRange = ThisWorkbook.Sheets("Sheet1").Range("A1")
    
             Chan = SQLOpen("DSN=NWind;FIL=dBase4;")
    
             ' Enter the following statement on a single line.
             NumCols = SQLExecQuery(Chan, "SELECT customer.CONTACT, " & _
               "customer.COMPANY FROM customer.dbf")
    
             NumRows = SQLRetrieve(Chan, DestRange, , , True)
    
             SQLClose(Chan)
    
             DestRange.Resize(NumRows + 1, NumCols).CreateNames Top:=True, _
                Left:=False
    
          End Sub 


  2. Switch to a worksheet.


  3. From the Tools menu, choose Macros, and then select the GetData macro.


Additional query words: 5.00a 5.00c

Keywords : kbcode kbprg xlquery SynFnc
Version : MACINTOSH:5.0,5.0a; WINDOWS:5.0,5.0c,7.0
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.