SQLExecQuery Function

Description

In Microsoft Excel for Windows 95, do not use SQLExecQuery and the other ODBC functions in the XLODBC.XLA add-in; use the objects, methods, and properties in the Data Access Objects (DAO) library instead.

SQLExecQuery executes a query on a data source with a connection that has been established using SQLOpen.

SQLExecQuery executes only the query. Use SQLRetrieve or SQLRetrieveToFile to get the results.

This function is contained in the XLODBC.XLA add-in (ODBC Add-In on the Macintosh). Before you use the function, you must establish a reference to the add-in using the References command (Tools menu).

Syntax

SQLExecQuery(connectionNum, queryText)

connectionNum

Required. The unique connection ID returned by SQLOpen that identifies the data source you want to query.

queryText

Required. The query to be executed on the data source. The query must follow the SQL syntax guidelines for the specific driver.

Return Value

The value returned by SQLExecQuery depends on the type of SQL statement executed, as shown in the following table.

SQL statement executed

Return Value

SELECT

The number of columns in the result set.

UPDATE, INSERT or DELETE

The number of rows affected by the statement.

Any other valid SQL statement

0 (zero)


If SQLExecQuery is unable to execute the query on the specified data source, it returns Error 2042.

If connectionNum is not valid, SQLExecQuery returns Error 2015.

Remarks

Before calling SQLExecQuery you must establish a connection to a data source using SQLOpen The unique connection ID returned by SQLOpen is used by SQLExecQuery to send queries to the data source.

If you call SQLExecQuery using a previously used connection ID, any pending results on that connection are replaced by the new results.

See Also

SQLBind Function, SQLClose Function, SQLError Function, SQLGetSchema Function, SQLOpen Function, SQLRequest Function, SQLRetrieve Function, SQLRetrieveToFile Function.

Example

This example runs a query on the NWind sample database. The result of the query, displayed on Sheet1, is a list of all products that are currently on order.


If Application.OperatingSystem Like "*Win*" Then
    databaseName = "NWind"
Else        'Macintosh
    databaseName = "NorthWind"
End If
queryString = "SELECT * FROM product.dbf WHERE (product.ON_ORDER<>0)"
chan = SQLOpen("DSN=" & databaseName)
SQLExecQuery chan, queryString
Set output = Worksheets("Sheet1").Range("A1")
SQLRetrieve chan, output, , , True
SQLClose chan