office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Access(VBA)連接Oracle數據庫的代碼

2019-06-17 16:21:00
tmtony8
原創
5856

access是包含前颱程序開髮和後颱數據庫的應用軟件。

可以利用access作爲前颱開髮,後颱用sqlServer,oracle這些作爲數據庫。

而access連接數據庫,可以利用鏈接錶。直接讀取錶記録。也可以通過ADO等數據操作方式讀取數據庫


方法1:odbc連接方式

      Dim OracleDatabase As String
        OracleDatabase = "DSN=ODBC源名稱;UID=你的用戶名;PWD=你的密碼;LANGUAGE=us_english; DATABASE=NOKIAKPI"
        Set conn = CreateObject("adodb.connection")
        conn.Open OracleDatabase
        If Err.Number <> 0 Then
            MsgBox ("Please Check connection to KPI Server!")
            DoCmd.Quit
        Else    

            KPITableName = "TEMPKB" & Format(Date, "ddhh24") & Format(Time, "hhss")
            LocalTable = rstrDesTbl
            '----------fetch table from oracle db-------------------
            SQL = "create table " & KPITableName & " as " & rstrSrcTbl    'nokiaadmin
            conn.CommandTimeout = 300
            conn.Execute (SQL)
            DoCmd.DeleteObject acTable, LocalTable
            '   KPITableName = "kpiview." & KPITableName
            DoCmd.TransferDatabase acTable, "ODBC Database", "ODBC;" & OracleDatabase, acTable, KPITableName, LocalTable
            conn.Execute ("DROP TABLE " & KPITableName & ";")

        End If
        Exit Function


方法2:oledb

Dim Cnn As Object
    Set Cnn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.recordset")
     rs.cursorlocation = 3
  cnn.Open "data source=服務器名;provider=OraOLEDB.Oracle;user id=用戶;password=密碼"
    Sql = "Select 字段名 from 錶名 where ID=1"
    rs.Open SQL, Cnn
  rs.Close
  Set Cnn = Nothing
  Set rs = Nothing
分享