Name

querytable.Connection[= setting]

Synopsis

Sets or returns the connection string for the specified query table. The following code creates a query table, returns its results, and displays the connection string in cell A6:

Dim strConn As String
Dim strSQL As String
Dim qt As QueryTable
 
strConn = "ODBC;DSN=MS Access Database;" & _
    "DBQ=C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb;"
 
Set qt = ActiveSheet.QueryTables.Add(Connection:=strConn, _
    Destination:=ActiveSheet.Range("A1"))
qt.CommandText = "SELECT * FROM Products WHERE (Products.ProductID=10)"
qt.Refresh
ActiveSheet.Range("A6") = qt.Connection

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.