我正嘗試使用VBA連接到Netezza。我啓用了以下內容:如何通過VBA連接到Netezza(用於分析的PureData系統)
- 的Microsoft Excel 15.0對象庫
- 的Microsoft Office 15.0對象庫
- Microsoft ActiveX數據對象6.1庫
- Visual Basic應用程序
這裏是我的代碼:
Sub NZConn()
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim x As Variant
Set cmd = New ADODB.Command
Set RS = New ADODB.Recordset
cmd.ActiveConnection = "Driver={Netezza " & _
"ODBC};servername=servername;port=####;database=database;" & _
"username=username;password=password;"
cmd.ActiveConnection.CursorLocation = adUseClient
cmd.CommandTimeout = 120
cmd.CommandType = adCmdText
x = "Write Query here"
cmd.CommandText = x
Set rs = cmd.Execute
Sheet1.Range("A1").CopyFromRecordset rs
cmd.ActiveConnection.Close
End Sub
我可以讓代碼在沒有返回錯誤的情況下運行,但是沒有任何東西是從記錄集粘貼的,這導致我認爲這可能與連接字符串的結構有關。
我有服務器,用戶名,密碼,數據庫,端口和驅動程序。
我需要先建立/打開ActiveConnection嗎?
嘗試'服務器名=服務器名,端口號' – Tehscript
從聲明中減去'新'作爲'Dim cmd As ADODB.Command'和'Dim rs As ADODB.Recordset'。並確保rs是小寫字母,作爲'Set rs = New ADODB.Recordset'。 – Tehscript
Driver = {NetezzaSQL}; servername = myServerAddress; port = myPortNumber; database = myDataBase; username = myUsername; password = myPassword –