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
324 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:DataTrain
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you - I have got this working now using DoCmd.TransferDatabase. I appreciate your help. David.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now