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
361 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 250 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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 250 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best laptop programming computer for Windows development 14 83
Extract data from output with RegEx 1 39
Calculation in a Report 13 36
COM server issue 2 18
In this post we will learn different types of Android Layout and some basics of an Android App.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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…

738 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