执行vba脚本时出现未指定的运行时错误

时间:2014-07-18 12:52:15

标签: excel oracle vba

我是vba的新手,我正在使用vba脚本从excel连接到数据库并获取记录。我为此编写了以下脚本。我得到运行时错误'-2147467259(80004005)':未指定错误。 如何解决此错误。请参阅错误屏幕截图。

 Sub Ora_Connection()
        Dim con As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim query As String

        Set con = New ADODB.Connection
        Set rs = New ADODB.Recordset
        '---  Replace below highlighted names with the corresponding values
        strCon = "Driver={Microsoft ODBC for Oracle}; " & _
        "CONNECTSTRING=(DESCRIPTION=" & _
        "(ADDRESS=(PROTOCOL=TCP)" & _
        "(HOST=host_name)(PORT=1521))" & _
        "(CONNECT_DATA=(SERVICE_NAME=service_name))); uid=id; pwd=pw;"
        '---  Open the above connection string.
        con.Open (strCon)
        '---  Now connection is open and you can use queries to execute them.
        '---  It will be open till you close the connection
        query = "select * from security.forms"
        Set rs = con.Execute(query)

        For i = 0 To rs.Fields.Count - 1
            Sheet1.Cells(1, i + 1).Value = rs.Fields(i).Value
        Next



        con.Close
        End Sub

错误截屏: Error Message

3 个答案:

答案 0 :(得分:0)

您是否尝试使用RecordSet实例的open方法?也许它会给你一个更有帮助的错误。

Dim connection As New ADODB.connection
Dim rst As New ADODB.Recordset
Dim query As String

connection.ConnectionString = CONNECTION_STRING
connection.Open

rst.Open query, connection, adOpenKeyset, adLockOptimistic

do while not rst.EOF
   rst.MoveNext
loop

connection.Close

答案 1 :(得分:0)

我遇到了完全相同的问题,很难找到答案,因为这个问题的大多数帖子都没有答案。

我通过使用另一个Oracle驱动程序解决了它。而不是 Microsoft ODBC for Oracle 尝试在OraClient11g_home1 驱动程序中使用 Oracle。希望这有帮助

答案 2 :(得分:-1)

请添加参考' Microsoft ActiveX Data Objects 2.8库'

enter image description here