5

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 data tables. I'd like to be able to set up the connection and link the tables from within the Access file. That way users don't have to worry about setting up an DSN for the ODBC connection (most of them are not very technical users, and will require hand-holding for something like this), and we can eventually have the Access file point to different test environments and generate reports.

Is it possible to have the database connection created dynamically when the file is opened, and can I dynamically change where my Linked Tables link to?

2 Answers 2

12

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
2
  • 1
    Note that the connection string format is different for every database out there. Look here for a reference: carlprothman.net/Default.aspx?tabid=81 Jun 23, 2010 at 11:37
  • 1
    If you don't need to be aware of what is happening in the UI layer of Access, then replace: Set DB = CurrentDb - to: Set DB = Workspaces(0).Databases(0) or to: Set DB = dbEngine(0)(0) Because it is considerably faster; as much as five thousand times faster. Oct 16, 2014 at 19:04
7

I do my programming on a workstation with a DSN defined, and then before distributing for production use, run a variant of Doug Steele's code to convert all the DSN-based connect strings to be DSN-less.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.