Export (0) Print
Expand All

SQLOpen Function

Office 2007

It is recommended that you use the objects, methods, and properties in the Microsoft ActiveX Data Objects (ADO) library instead of SQLOpen and the other ODBC functions in the Xlodbc.xla add-in.

SQLOpen establishes a connection to a data source.

This function is contained in the Xlodbc.xla add-in. Before you use the function, you must establish a reference to the add-in by using the References command (on the Tools menu) in the Visual Basic Editor.

SQLOpen(ConnectionStr, OutputRef, DriverPrompt)

1The driver dialog box is always displayed.
2The driver dialog box is displayed only if information provided by the connection string and the data source specification aren’t sufficient to complete the connection. All dialog box options are available.
3The same as 2 except that dialog box options that aren’t required are dimmed (unavailable).
4The driver dialog box isn’t displayed. If the connection isn’t successful, SQLOpen returns an error.

Return Value

If successful, SQLOpen returns a unique connection ID number. Use the connection ID number with the other ODBC functions.

If SQLOpen is unable to connect using the information you provide, it returns Error 2042. Additional error information is placed in memory for use by SQLError.


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

DatabaseName = "Northwind"
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

Community Additions

© 2014 Microsoft