Programatically create ODBC connection and link tables in MS Access

前端 未结 2 597
无人及你
无人及你 2021-02-10 11:00

We are using MS Access as a reporting front-end for Oracle. The data is accessed via an ODBC connection, and there are two linked tables in Access to Oracle that are the raw dat

2条回答
  •  忘了有多久
    2021-02-10 11:17

    You want a DSN-less linked table connection from Access. It is possible and I've done it but I don't have the code with me. I think it was something like the below (this uses a SQL Server source but Oracle would just have a slightly different connection string). To have the table(s) created on startup you'll need to check for the existence of each tabledef prior to attempting to create them again and call a subroutine like the below upon Access database open.

    Function LinkTables()
        Dim DB As Database, tDef As TableDef
        Set DB = CurrentDb
        Set tDef = DB.CreateTableDef("YourAccessLinkedTableNameHere")
        tDef.Connect = "ODBC;Driver={SQL Server};Server=srvname;Database=dbname;UID=sqluserid;PWD=sqlpwd"
        tDef.SourceTableName = "dbo.YourSourceTableNameHere"
        DB.TableDefs.Append tDef
    End Function
    

提交回复
热议问题