Results 1 to 12 of 12

Thread: Programatically change linked tables

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Programatically change linked tables

    Hello, all,

    Wondering if there is a way to do this...
    I have a front end user application in Access and two back ends. The two backends are identical. The difference between the two back ends are that one is for development and the other is live (users hit this via the front end). Normally the front end is linked to the live back end. There are times when I need to do developmental work and I want to hit the developmental back end for testing.

    Normally, I can use the linked table manager to relink my front end to the developmental back end. There are alot of linked tables so this becomes a time consuming process to relink the tables every time.

    I was wondering if there is a way to do this via code? Then all I'd have to do is hit a button and it would link me to the developmental back end or the live back end.

    Thanks in advance!

    Strick

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Programatically change linked tables

    stricknyn,

    In Access there is a re-link function from the menu. Go to Tools->Database Utilities->Linked Table Manager. This will allow you to change all your linked tables to the other database with Just a click.

    There is also a way to do this function by code. I dont remember it right now but I will see if I can find the code.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Programatically change linked tables

    Hello,

    Yes, that's the way I normally do it. I was looking for a way of doing it via code.

    Strick

  4. #4
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Programatically change linked tables

    Here's some code using ADO & ADOX to link external tables. I also have the DAO equivalent (somewhere) if you're interested.
    VB Code:
    1. Public Sub AttachTable(ByVal srcDB As String, ByVal srcTable As String, _
    2.     ByVal destDB As String, Optional ByVal destTable As String = "")
    3.     '
    4.     '   Create a link to a table in a database.
    5.     '   If destTable > "" then rename the table in the destination database
    6.     '
    7.     Dim cat As ADOX.Catalog
    8.     Dim con As ADODB.Connection
    9.     Dim tbl As ADOX.Table
    10.    
    11.     Set cat = New ADOX.Catalog
    12.     Set con = New ADODB.Connection
    13.     Set tbl = New ADOX.Table
    14.    
    15.     con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & destDB
    16.    
    17.     Set cat.ActiveConnection = con
    18.     '
    19.     '   Determine whether we're renaming the destination table
    20.     '
    21.     If Len(destTable) > 0 Then
    22.         tbl.Name = destTable
    23.     Else
    24.         tbl.Name = srcTable
    25.     End If
    26.     Set tbl.ParentCatalog = cat
    27.     '
    28.     '   Set the link properties
    29.     '
    30.     tbl.Properties("Jet OLEDB:Create Link") = True
    31.     tbl.Properties("Jet OLEDB:Link Datasource") = srcDB
    32.     tbl.Properties("Jet OLEDB:Remote Table Name") = srcTable
    33.     '
    34.     '   Append the table to the collection and we're done
    35.     '
    36.     cat.Tables.Append tbl
    37.  
    38.     Set cat = Nothing
    39.     Set tbl = Nothing
    40.    
    41.     con.Close
    42.     Set con = Nothing
    43.  
    44. End Sub
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  5. #5
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Programatically change linked tables

    stricknyn,


    I found the code. It's verrrry old but should still work. It opens a table with all the table names and the database that they are in, then attaches to that database and links the tables for all names in the table.

    VB Code:
    1. Function SetupAttachedTables() As Integer
    2.  
    3.     Dim TblName As String
    4.     Dim dbCurrent As Database
    5.     Dim dbTable As Recordset
    6.     Dim Errors As Integer
    7.  
    8.         Set dbCurrent = CodeDb()
    9.         Set dbTable = dbCurrent.OpenRecordset("Attached Tables", dbOpenDynaset)
    10.  
    11.         dbTable.MoveFirst
    12.  
    13.   '      On Error GoTo CountAttachErrors
    14.         Errors = 0
    15.  
    16.         Do While Not dbTable.EOF
    17.  
    18.             TblName = dbTable![Table Name]
    19.             dbName = dbTable![Database]
    20.  
    21.             DeleteTable TblName              'Delete old table reference
    22.  
    23.             DoCmd.TransferDatabase A_ATTACH, "Microsoft Access", dbName, acTable, TblName, TblName
    24.             dbTable.Edit
    25.             dbTable![Attached] = Now
    26.             dbTable.Update
    27.  
    28.             dbTable.MoveNext
    29.  
    30.         Loop
    31.  
    32.         dbTable.Close
    33.         SetupAttachedTables = Errors
    34.         Exit Function
    35.  
    36. CountAttachErrors:
    37.        
    38.         Errors = Errors + 1
    39.         Resume Next
    40.  
    41. End Function

  6. #6
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Programatically change linked tables

    Hi,

    Not sure about the DAO code there but what you want to do is something like this (possibly)

    1) Delete all linked tables
    2) Open the other (external) db (test or live)
    3) Loop through and link tables in the current db, using the table names from the external db

    ** Note: you will probably not need to link the "msys*" tables

    If you are using DAO then please try a looping code first. If you are still having problems, then post up your code for assistance/guidance.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Programatically change linked tables

    Thanks, guys,

    I'll try this code out when I get home and let you know how it turns out.

    Strick

  8. #8
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    330

    Re: Programatically change linked tables

    How did the code work?

    I am looking for an ADO option for relinking tables. I have a DAO version but I am looking for ADO version.

    Thanks,

    Swoozie
    Swoozie
    Somedays you just should not get out of bed.

  9. #9
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Programatically change linked tables

    Have a look at post #4 in this thread....
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  10. #10
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    330

    Re: Programatically change linked tables

    Yeah, That is the one I have been looking at. I just wanted to know if everything worked, or if any issue were come accross. I was trying to use something similar and had issues. however the code is #4 seemed simpler than what I had so before I removed my working DAO code I just wanted feed back on what the outcome of this post was.

    Swoozie
    Swoozie
    Somedays you just should not get out of bed.

  11. #11
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Programatically change linked tables

    That code is basically what I use and it works fine. You will probably want to add some error handling though.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  12. #12
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: Programatically change linked tables

    Personally I would never relink a development database with live one.

    I normally create a completely seperate folder or drive area for testing databases and the live system.

    When making changes to the data database just copy over the new objects to live and replace em or get users out...fill em with live data.

    The trouble with the relink code is there is a chance you will forget where your Frontend is pointing...and you could plough on doing development to Live system.

    I just wouldnt take any risk......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width