2013-07-27 151 views
0

問候..VBA到Oracle 10g錯誤

我對Oracle很陌生,但在Sql中有知識。

我試圖外部Oracle DB從VBA連接 - Excel的下面是代碼..

Function ORAQUERY(strHost As String, strDatabase As String, strSQL As String, 
strUser As String, strPassword As String) 
Dim strConOracle, oConOracle, oRsOracle 
Dim StrResult As String 
StrResult = "" 
strConOracle = "Driver={Microsoft ODBC for Oracle}; " & _ 
"CONNECTSTRING=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=207.169.236.29)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=DEDWRP01.DE.EDS.COM))); uid=" & strUser & " ;pwd=" & strPassword & ";" 
Set oConOracle = CreateObject("ADODB.Connection") 
Set oRsOracle = CreateObject("ADODB.Recordset") 
oConOracle.Open strConOracle 
End Function 

試圖運行上面的代碼我得到下面的錯誤..

[Microsoft] [用於Oracle的ODBC驅動程序] [Oracle] ORA-12154:TNS:Cound未解析指定的連接標識

我已經安裝了oracle 10g,並且我能夠通過連接數據庫在SQL * Plus ..

enter image description here

請幫我.Thanks對於提前幫助..

+1

您是否嘗試明確設置tnsnames條目,並創建一個DSN和teste從ODBC管理面板中查看它?我會首先嚐試排除ODBC安裝問題。 – OldProgrammer

+0

感謝您的評論...請讓我知道我該怎麼做?新的。:(請 –

+1

請檢查http://www.connectionstrings.com/oracle/。在此頁面中查找「Microsoft ODBC」以獲取不同的連接方式,具體取決於您想要的方法。 – shahkalpesh

回答

0

這是我用一個生動的代碼:

Dim oCon As ADODB.Connection 
Dim oRS As ADODB.Recordset 
Set oCon = New ADODB.Connection 
oCon.ConnectionString = "DSN=YOUR_DNS;UID=YOUR_USERNAME;PWD=YOUR_PASSWORD;APP=Microsoft Office 2003;WSID=YOUR_WSID" 
oCon.Open 
Set oRS = New ADODB.Recordset 
oRS.ActiveConnection = oCon 

'Loop through Oracle 
    oRS.Source = "select DATA from TABEL;" 
    oRS.Open 

     Do While Not oRS.EOF 
      Debug.Print oRS.Fields("DATA").Value 
      oRS.MoveNext 
     Loop 

    oRS.Close 

oCon.Close 


If Not oRS Is Nothing Then Set oRS = Nothing 
If Not oCon Is Nothing Then Set oCon = Nothing 

連接到Oracle - 請記住激活工具 - >參考 - >「Microsoft ActiveX數據對象2.8庫」