아래는 Excel에서 매개 변수화 된 쿼리를 작성해야하는 코드입니다. MS Excel 2013을 실행 중입니다. 현재 SQL Server 데이터베이스에 연결하려고합니다. 여기에서 필자는 열 값을 입력하고 열의 모든 행 (WHERE 절)에 대해 데이터베이스를 쿼리하는 단일 셀을 사용하여이 데이터베이스를 쿼리하려고합니다. 이 셀은 동적이기 때문에 값을 변경하면 쿼리의 결과가 변경됩니다. 때 .refresh 섹션에서VBA를 사용하는 매개 변수로 Excel에서 테이블 쿼리
With qt
.CommandText = sSQL
.CommandType = xlCmdSql
.AdjustColumnWidth = True 'add any other table properties here
.BackgroundQuery = False
.Refresh
End With
내가 오류가 계속 : 저는 여기에서
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
이 코드입니다. 누구든지 도와 줄 수 있습니까? 다음은 내 DB에 대한 링크입니다 Database link
SQL Server Express를 실행하고 서버는. \ SQLEXPRESS입니다. 누구든지 도움을받을 수 있다면 크게 감사하겠습니다.
어떤 오류가 발생합니까? 런타임 오류 '1004': 일반 ODBC 오류 내가 최근에 비슷한 일을 시도했습니다 –
나는이 오류가 계속 '티. –
과'Add' didn를 어디에, Paramterer.Append' 근무''CreateParameter'를 사용하고 있음을 발견 –