Unable to connect to Teradata from excel using VBA code - Teradata Server can't be reached over the network
Solution 1
What if you change the Connection object's Open method:
conn.Open "DSN=dsnname;Databasename=dbname;Uid=Userid;Pwd=****;"
Solution 2
This connection script worked for me.
' Add Microsoft ActiveX Data Objects 2.8 Library in References
' When installing Teradata SQL Assistant, include the ODBC Driver for Teradata will install the TDOLEDB provider
' This example connects to Teradata, deletes the contents of MyTable & inserts row 7- 8 from the active spreadsheet
Function OpenConn() As Object
Set OpenConn = New ADODB.Connection
Dim myConnectionString As String
myConnectionString = "Provider=TDOLEDB;Data Source=MyTeradataServerName;Persist Security Info=True;User ID=MyTeradataUserID;Password=MyTeradataPass;Session Mode=ANSI;DefaultDatabase=GRP_BCE_FINANCE_IM;MaxResponseSize=65477;"
OpenConn.Open myConnectionString
End Function
Sub CloseConn(conn As Object)
conn.Close
Set conn = Nothing
End Sub
Sub PushCCHier()
Dim TeraObjCmd As New ADODB.Command
Dim TeraObjRs As ADODB.Recordset
Dim TeraObjRs2 As ADODB.Recordset
Dim TeraCnxn As Object
Set TeraCnxn = OpenConn()
TeraObjCmd.ActiveConnection = TeraCnxn
'Clear Previous Data
TeraObjCmd.ActiveConnection = TeraCnxn
TeraObjCmd.CommandText = "delete from MyTable"
TeraObjCmd.Execute
'Load New Data
Set TeraObjRs2 = New ADODB.Recordset
TeraObjRs2.Open "SELECT * FROM MyTable where 1 = 2 ", TeraCnxn, adOpenStatic, adLockOptimistic
With TeraObjRs2
For irow = 7 To 8 'loading results from rows in my spredsheet
If Len(Trim(Range("B" & irow).Value)) <> 0 Then 'Avoid blank rows
.AddNew
.Fields(0) = Range("B" & irow).Value
End If
Next
.UpdateBatch
.Close
End With
' clean up objects
Set objCmd = Nothing
Call CloseConn(TeraCnxn)
MsgBox "Update Complete!"
End Sub
Admin
Updated on June 23, 2022Comments
-
Admin almost 2 years
I have been trying to connect to Teradata from Excel using vba code but I am getting the below mentioned error: Teradata Server can't be reached over the network. I have been able to successfully connect from Teradata SQL assistant and I also successfully pinged the Teradata server(I am using Teradata express edition in my laptop which runs on VMware player). I also successfully connected to Teradata from Excel Data connection wizard and Microsoft query. I have also switched off the firewall and checked to see If I am able to connect but still no luck in connecting. Please let me know where I am going wrong.
Please find below code:
Private Sub CommandButton1_Click() Dim conn As ADODB.Connection Dim rec1 As ADODB.Recordset Dim thisSql As String Set conn = New ADODB.Connection conn.Open "Driver=Teradata;DBCName=dsnname;Databasename=dbname;Uid=Userid;Pwd=****;" thisSql = "sel * from customer_db.customer" Set rec1 = New ADODB.Recordset rec1.Open thisSql, conn With Sheet1.QueryTables.Add(Connection:=rec1, Destination:=Sheet2.Range("A1")) .Name = "data" .FieldNames = True .Refresh BackgroundQuery:=False End With End Sub