À l'aide de VBA pour interroger une table SQL Server dans Excel

Je suis en train d'interroger une table dans Microsoft Excel à l'aide de VBA. J'ai écrit du code pour essayer d'accomplir cette tâche, mais je reçois un message d'erreur:

erreur d'exécution '1004': Dire que c'est un Général d'erreur ODBC.

Je ne suis pas sûr de ce que je dois faire pour obtenir ce code fonctionne correctement donc je peux interroger ce tableau.

Je suis à l'aide de SQL Server Express, le serveur je me connecte à: .\SQLEXPRESS

Base de données:

Databaselink

L'interrogation de la table produits
Code VBA:

Sub ParameterQueryExample()
'---creates a ListObject-QueryTable on Sheet1 that uses the value in 
'        Cell Z1 as the ProductID Parameter for an SQL Query
'        Once created, the query will refresh upon changes to Z1. 

Dim sSQL As String
Dim qt As QueryTable
Dim rDest As Range

'--build connection string-must use ODBC to allow parameters
Const sConnect = "ODBC;" & _
    "Driver={SQL Server Native Client 10.0};" & _
    "Server=.\SQLEXPRESS;" & _
    "Database=TSQL2012;" & _
    "Trusted_Connection=yes"

'--build SQL statement
sSQL = "SELECT *" & _
        " FROM TSQL2012.Production.Products Products" & _
        " WHERE Products.productid = ?;"

'--create ListObject and get QueryTable
Set rDest = Sheets("Sheet1").Range("A1")
rDest.CurrentRegion.Clear  'optional- delete existing table

Set qt = rDest.Parent.ListObjects.Add(SourceType:=xlSrcExternal, _
    Source:=Array(sConnect), Destination:=rDest).QueryTable

With qt.Parameters.Add("ProductID", xlParamTypeVarChar)
    .SetParam xlRange, Sheets("Sheet1").Range("Z1")
    .RefreshOnChange = True
End With

'--populate QueryTable
With qt
    .CommandText = sSQL
    .CommandType = xlCmdSql
    .AdjustColumnWidth = True  'add any other table properties here
    .BackgroundQuery = False
    .Refresh
End With

Set qt = Nothing
Set rDest = Nothing
End Sub
Jetez un oeil ici: http://www.connectionstrings.com/sql-server/. Il existe peu de moyens pour se connecter à sql server et quelques fournisseurs... Essayez différents paramètres de connexion.
De ligne cela se produit-il? nous jeter un indice.

OriginalL'auteur Mitchell Walker | 2013-07-15