通过excel vba为oracle建立无连接的连接

时间:2013-09-26 20:56:06

标签: oracle excel vba

如何通过excel vba为oracle db创建无dsn连接。

这里是我在访问vba中的方式(我有字符串中的变量函数,GetOracleDriver和TNSName,因为每台机器都有自己的)但是在excel中我没有看到oracle驱动程序,当我尝试做手工制作。

Dim NewConnect As String

NewConnect = "ODBC;DRIVER={" & GetOracleDriver & "};SERVER=" & TNSName & ".mycompany.com;UID=myuid;PWD=mypwd;DBQ=" & TNSName & ".mycompany.com;"

谢谢

1 个答案:

答案 0 :(得分:3)

我道歉,这是我发布的另一个问题,然后找到答案

以下代码取自http://www.vbaexpress.com/forum/showthread.php?26968-How-to-extract-data-from-Oracle-Database-into-Excel-Spreadsheet-via-VBA-code&p=186731&viewfull=1#post186731

并修改了一点

Public Sub ImportData()
    Const strSQL_c As String = "SELECT * from NR_CF_CF3_DIR"
    Dim strConnection As String
    Dim strDBPath As String
    strConnection = "ODBC;DRIVER={Oracle in Oracle1};SERVER=mytnsname.mycompany.com;UID=myuid;PWD=mypwd;DBQ=mydbname.mycompany.com"

    ActiveWorkbook.Worksheets.Add
    ActiveSheet.Name = "QCF3_DIR"


    QueryDB strConnection, ActiveSheet.Cells(1, 1), strSQL_c
End Sub






Public Sub QueryDB(ByVal connectionString As String, ByVal target As Excel.Range, ByVal SQL As String)
    Dim qt As Excel.QueryTable
    Dim ws As Excel.Worksheet
    Set ws = target.Parent
    Set qt = ws.QueryTables.Add(connectionString, target, SQL)
    qt.Refresh BackgroundQuery:=False
End Sub