在没有DSN的Teradata Access中创建链接表?

时间:2017-08-30 21:34:54

标签: ms-access database-connection teradata

与Teradata的连接

'Requires reference to ADO and ADOX  
Public adoCn As ADODB.Connection
Public adoCat As New ADOX.Catalog
Public adoTbl As New ADOX.Table

Function TD_Make_Linked_Table()

Dim varServer As String
Dim varDatabase As String
Dim varUser As String
Dim varPassword As String

varServer = "Test"
varDatabase = "Test_Test"
varUser = "Test_User"
varPassword = "Test_Password"

Set adoCn = New ADODB.Connection

'I have tried multiple connection strings

adoCn.ConnectionString = "PROVIDER=MSDASQL;DRIVER={Teradata};" & _
                         "DBCName=" & varServer & ";" & _
                         "DefaultDatabase=" & varDatabase & ";" & _
                         "UID=" & varUser & ";" & _
                         "PWD=" & varPassword & ";"

adoCn.Open

Set adoCat = New ADOX.Catalog
Set adoCat.ActiveConnection = adoCn
Set adoTbl = New ADOX.Table

adoTbl.ParentCatalog = adoCat
adoTbl.Name = "Test"

'I have tried multiple property combinations
'Causes error 3265 Item not found
'adoTbl.Properties("?") = adoCn
'adoTbl.Properties("Jet OLEDB:Link Datasource") = "Test"
'adoTbl.Properties("Jet OLEDB:Link Provider String") = 
'adoTbl.Properties("Jet OLEDB:Remote Table Name") = "LinkDatabaseTable"
'adoTbl.Properties("Jet OLEDB:Create Link") = True

'Causes 3251 provider is not capable of performing operation
'adoCat.Tables.Append adoTbl

adoCn.Close

   Set adoTbl = Nothing
   Set adoCat = Nothing
   Set adoCn = Nothing

End Function

我已验证连接正常。我可以通过打开连接并执行SQL来查询数据。

我无法使用Visual Basic以编程方式在Access中为已创建连接的应用程序创建链接表。

是否有人能够从Access到Teradata成功创建链接的无dsn表?

2 个答案:

答案 0 :(得分:1)

如果您有一个有效的ODBC连接字符串,那么您应该能够使用DoCmd.TransferDatabase来创建链接表。我没有Teradata服务器,但这适用于SQL Server:

Dim connStr As String
connStr = _
        "DRIVER=ODBC Driver 11 for SQL Server;" & _
        "SERVER=(local)\SQLEXPRESS;" & _
        "DATABASE=myDb;" & _
        "Trusted_Connection=yes"
DoCmd.TransferDatabase _
        TransferType:=acLink, _
        DatabaseType:="ODBC Database", _
        DatabaseName:="ODBC;" & connStr, _
        ObjectType:=acTable, _
        Source:="dbo.projects", _
        Destination:="dbo_projects"

答案 1 :(得分:0)

我最终找到了一个有效的连接字符串! :)

Function adoTera()
Dim oConn As ADODB.Connection
Dim rs As ADODB.Recordset

Set oConn = New ADODB.Connection
Set rs = New ADODB.Recordset

oConn.ConnectionString = "PROVIDER=MSDASQL;" & _
"DRIVER={Teradata};" & _
"DBCName=yourDBCName;" & _
"DefaultDatabase=yourDBName;" & _
"UID=yourUserName;" & _
"PWD=yourPassword;"

oConn.Open
rs.Open "SELECT * FROM yourTable", oConn

ThisWorkbook.Worksheets("Sheet1").Range("A:A").CopyFromRecordset rs
rs.Close
oConn.Close

Set rs = Nothing
Set con = Nothing

End Function