Microsoft KB Archive/117644

= SQLRetrieve Defines Field Names on Active Workbook =

Article ID: 117644

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0c
 * Microsoft Excel 5.0a for Macintosh

-



This article was previously published under Q117644



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.



Visual Basic Code Example
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. 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:

  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  Switch to a worksheet. From the Tools menu, choose Macros, and then select the GetData macro.

Additional query words: 5.00a 5.00c XL

Keywords: kbcode kbhowto kbprogramming KB117644

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.