Chapter 19 - Understanding Client-Server Development with VBA in Access 2007 VBA Programmer's Reference has a great section on Using Linked Tables in ACCDB/MDB Files. This covers another way of creating a DSN, and then linking tables as needed in code.
Here is the code to create the DSN:
Public Sub CreateNewDSN()
' Define Variables
Dim strDSNName As String
Dim strDriverName As String
Dim strDescription As String
Dim strServer As String
Dim strDatabase As String
' This is the DSN name to use when
' referencing the DSL in your code
strDSNName = "TestDSN"
' The name of the ODBC Driver used for the connection
strDriverName = "SQL Server"
' This is the optional description to use
' in the ODBC Driver Manager program
strDescription = "Test DSN Description"
' In the case of SQL Server, use the following
' line of code to specify the SQL Server to connect to
strServer = "<machine name>\<sqlexpress>"
' Then name of the Default database on the server
' used for this DSN. If not specified, then SQL Statements
' may end up getting executed against the master database
strDatabase = "NorthwindCS"
' Create the DSN
DBEngine.RegisterDatabase _
strDSNName, strDriverName, _
True, "Description=" & strDescription & _
Chr(13) & "Server=" & strServer & _
Chr(13) & "Database=" & strDatabase
End Sub
Then to link tables:
Public Sub CreateLinkedTable()
' NOTE: This code requires the DAO object library to work.
' If you are using the SampleACCDB.accdb sample file, then this
' reference should already be present. When unsure, check the
' VBA project references under Tools -> References menu option
' in the VBA editor and make sure a reference is set to:
' 1. Access 2007: Microsoft Office 2007 Access database engine Object Library
' 2. Access 2003 and older: Microsoft DAO 3.6 Object library
' Define Variables
Dim strConnection As String
Dim daoTableDef As DAO.TableDef
' This must reference an existing DSN
Const strDSNName = "TestDSN"
' The application name can be used for tracing and
' troubleshooting the source of problems on the server.
' This can be anything, but usually the more specific the better.
Const strAppName = "Microsoft Office Access 2007"
' The database where the table resides on SQL Server
Const strDatabase = "NorthwindCS"
' User name for logging into the database server. This could be
' captured by a logon form and stored in a global variable.
Const strUserName = "sa"
' Password for logging in to the database server. This could
' be captured by a logon form and stored in a global variable.
Const strPassword = "password"
' Then name of the table on the remote server
Const strRemoteTableName = "Customers"
' The name of the table we want create in the local file
' that links to the Remote Table
Const strLocalTableName = "dbo_Customers"
' This will build the ODBC connection string for our new table
strConnection = _
"ODBC:" & _
"DSN=" & strDSNName & ";" & _
"APP=" & strAppName & ";" & _
"DATABASE=" & strDatabase & ";" & _
"UID=" & strUserName & ";" & _
"PWD=" & strPassword & ";" & _
"TABLE=" & strRemoteTableName
' This creates a new table object and adds it to the local
' database. If your tables already exist, then you would
' skip this code and use code to refresh the links, instead
Set daoTableDef = CurrentDb.CreateTableDef( _
strLocalTableName, _
dbAttachSavePWD, _
strRemoteTableName, _
strConnection)
CurrentDb.TableDefs.Append daoTableDef
' Clean up
Set daoTableDef = Nothing
End Sub
Or to refresh links:
Sub RefreshTable()
' Define Variables
Dim daoTableDef As DAO.TableDef
' The name of the local linked table to refresh
strLocalTableName = "dbo_Customers"
' This will build the ODBC connection string for our new table
strConnection = _
"ODBC:DSN=TestDSN;APP=Microsoft Office Access 2007;" & _
"DATABASE=NorthwindCS;UID=sa;PWD=password;TABLE=Cu stomers"
' This code assumes that the linked table object have
' already been created and only need to be refershed.
Set daoTableDef = CurrentDb.TableDefs(strLocalTableName)
daoTableDef.Connect = strConnection
daoTableDef.RefreshLink
' Clean up
Set daoTableDef = Nothing
End Sub
I am not familiar with UDL files.
mmcdonal
Look it up at:
http://wrox.books24x7.com