?
Solved

How can I use VBA to automatically link via ODBC to a Database and Table from Access 2007?

Posted on 2009-04-02
7
Medium Priority
?
367 Views
Last Modified: 2013-11-27
I am using Access 2007. I have created an ODBC connection. Using the External Data tab I can manually select my ODBC connection and link to a Table in an external database. Is it possible to do the same in VBA so that I can AUTOMATICALLY open the ODBC connection and AUTOMATICALLY link to the Table, or more than one Tables? If you can help with the code and briefly explain what the code does/how it works, that would be very helpful! Thanks. David.
0
Comment
Question by:DataTrain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 1000 total points
ID: 24048251
the command to use to link tables is DoCmd.TransferDatabase

If you know the tablename, you can link directly like this


DoCmd.TransferDatabase acLink, "ODBC Database", sConnect, acTable, sLocalTable, sRemoteTable

where sConnect is the connection string to your ODBC connection
acTable is the type of object to link
sLocalTable is the name of the table you want it called in Accss
sRemoteTable is the name of the table on the remote db

Given that you dont know the tables, you could make a connection then iterate thru the tables

I think I have sample code somewhere, let me see if I can find it
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24048287
what is your backend db?

Here is an example, depending on your db, it may end up creating schema owners in the tablename.

I think this should do it though. Using ADOX, we make a connection to your ODBC then list tables then connect


    Dim adoConn As adodb.Connection
    Dim adoRecSet As adodb.Recordset
    Dim sSql As String
   
   
    Set adoConn = New adodb.Connection
    adoConn.Open "ODBCDSNName", "USERID", "PASSWORD"

    Set adoCat = CreateObject("ADOX.Catalog")
    Set adoCat.ActiveConnection = adoConn

    For Each adoTbl In adoCat.Tables
        DoCmd.TransferDatabase acLink, "ODBC Database", "CONNECTIONSTRING", acTable, adoTbl.Name, adoTbl.Name, False
    Next

    Set adoCat = Nothing
    adoConn.Close
0
 
LVL 28

Expert Comment

by:TextReport
ID: 24048314
You need to know what your ODBC coinnect string would be, once you know that then you can manipulate the Connect String of a TableDef or even create a new tabledef.

To find the ODNC connect string design the attached table and take a look at the DESCRIPTION property, the DESCRIPTION property will show you the CONNECTION and SOURCETABLENAME properties, the CONNECTION is everything beore ;Tablename=

Cheers, Andrew
Private Sub cmdDAOConnectString_Click()
Dim db As DAO.Database
Dim td As DAO.TableDef
 
    Set db = CurrentDb
    
' Change Existing Table
    Set td = db.TableDefs("Example_AttachedTableName")
    td.Connect = "ODBC;DSN=COMPAQ;Trusted_Connection=Yes;APP=2007 Microsoft Office system;DATABASE=EETest"
    td.RefreshLink
    
' Create New Table
    Set td = db.CreateTableDef("Example_AttachedTableName1")
    td.Connect = "ODBC;DSN=COMPAQ;Trusted_Connection=Yes;APP=2007 Microsoft Office system;DATABASE=EETest"
    td.SourceTableName = "tblB"
    db.TableDefs.Append td
    RefreshDatabaseWindow
 
End Sub

Open in new window

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:DataTrain
ID: 24048427
Thanks for your quick response. My backend database is SQL Server 2005. I will need a little time to work out my ODBC connect string and try a few things out.
0
 
LVL 28

Expert Comment

by:TextReport
ID: 24048444
My example is using a DSN to SQL Server with Trusted connections.
Cheers, Andrew
0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 1000 total points
ID: 24048494
have a look here at connection strings. Useful site
http://www.connectionstrings.com/

for sql server  2005 it is http://www.connectionstrings.com/sql-server-2005

One connection string I have, kept as reference but dont have sql server anymore is this

    sConnect = "ODBC;Driver={SQL Server};SERVER=servernamehere;DATABASE=dbnamehere;UID=useridhere;PWD=pswdhereifone;"

you should hopefully be able to use this in the transferdatabase call
0
 

Author Closing Comment

by:DataTrain
ID: 31565717
Thank you - I have got this working now using DoCmd.TransferDatabase. I appreciate your help. David.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question