Sprawdzanie SQL Server w Excelu za pomocą sparametryzowanego zapytania za pomocą VBA

Próbuję wysłać zapytanie do tabeli w programie Microsoft Excel za pomocą VBA. Napisałem kod, aby spróbować wykonać to zadanie, ale wciąż otrzymuję błąd czasu wykonania „1004”, mówiąc, że jest to ogólny błąd ODBC. Nie jestem pewien, co muszę zrobić, aby kod działał poprawnie, abym mógł wysłać zapytanie do tej tabeli.

Używam SQL Server Express, serwera, z którym się łączę:.\SQLEXPRESS

Baza danych:

Link do bazy danych

Sprawdzanie tabeli produktów Kod 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


'--add Parameter to QueryTable-use Cell Z1 as parameter
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

questionAnswers(1)

yourAnswerToTheQuestion