1

如何通过 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;"

谢谢你

4

1 回答 1

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
于 2013-09-26T21:26:42.497 回答