Edit "TableDef.Connect" vs Delete/Append new TableDef (1 Viewer)

Mitch_____W

Mitch
Local time
Yesterday, 21:05
Joined
Oct 31, 2001
Messages
73
Is it possible to "Edit" a TableDef.Connect value vs Creating a new TableDef with the connection string you desire, then deleting the old TableDef and appending the new TableDef? This would be a TableDef in a remote mdb.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 02:05
Joined
Sep 12, 2006
Messages
15,660
i've never tried to just change the string, but have a go

if you could edit the string, then i dont think you would need to delete/append the tabledef -although i'm not sure - you may need to refresh the tabledefs collection

note also that its not just the connect string - the connected table name needs to match also
 

ecawilkinson

Registered User.
Local time
Today, 02:05
Joined
Jan 25, 2005
Messages
112
hi,

you may find this bit of code useful. If you have a database split into a front-end and back-end (good practice), and the back-end is not in a fixed location, but just needs to be relative to the front-end, then this code ensures that, if you ever move the FE and BE that the link to the tables in the BE are updated automatically. Normally, when you create a link to a table in another database, Access hard-codes the link in the .Connect property of the tabledef. This means for instance that if you have your FE and BE located say in C:\database\ and you have to move them to, say, H:\database\ for whatever reason, you will find that the FE can no longer find its linked data. This is where the code below comes in handy.
Code:
' Links tables (that are linked already) to a backend DB dynamically and relative to the front-end. When tables are linked by Access,
' the path to the link is saved as a an absolute path, meaning that if the DB and backend are ever moved, then the links all break.
' CAVEAT - for this to work, the backend DBs for the linked tables must be in the same directory, but not necessarily the same directory
'          as the frontend. (If it is in a different directory then it must be in a sub-directory of the frontend DB's directory.
'
Public Function LinkTables()
    Dim Tdf As TableDef                          ' table definition
    Dim iLoop As Integer, tblCtr                 ' loop counter, counts the number of tables
    Dim sFE_Path As String, sBE_Path As String   ' frontend path, backend path
    Dim sBE_DB As String                         ' name of the backend MDB file
    
' Get the path of this database, i.e. the front-end
    With Application.CodeDb
        sFE_Path = Left(.Name, InStr(.Name, Dir(.Name)) - 1)
    End With
    
' if the backend is in a different directory, it would be defined here
' e.g. sBE_Path = sFE_Path & "backend\"
    
    sBE_Path = sFE_Path
    
'Loop through the tables collection, updating each link
    For Each Tdf In CurrentDb.TableDefs
       If Tdf.SourceTableName <> "" Then
            ' cuts down processing time if all the tables link to the same backend MDB
            If tblCtr = 0 Then
                sBE_DB = GetDB(Tdf.Connect)
            End If
            tblCtr = tblCtr + 1
            
            ' only updates link the DBs have been moved
            If (Right(Tdf.Connect, Len(Tdf.Connect) - 10)) <> (sBE_Path & sBE_DB) Then
                Tdf.Connect = ";DATABASE=" & sBE_Path & sBE_DB
                Tdf.RefreshLink 'Refresh the link
            End If
        End If
    Next 'Goto next table

End Function
' Gets the MDB filename without the directory information.
Private Function GetDB(strFullPath As String) As String
    Dim I As Integer
    
' I used this loop instead of using InstrRev, so that the code is compatible with Access '97 still. (InStrRev did not appear until v2000)
    For I = Len(strFullPath) To 1 Step -1
        If Mid(strFullPath, I, 1) = "\" Then
            GetDB = Right(strFullPath, Len(strFullPath) - I)
            Exit For
        End If
    Next

End Function

HTH,
Chris
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 02:05
Joined
Sep 12, 2006
Messages
15,660
there you are - solved by chris

Code:
[COLOR="Red"]            If (Right(Tdf.Connect, Len(Tdf.Connect) - 10)) <> (sBE_Path & sBE_DB) Then
                Tdf.Connect = ";DATABASE=" & sBE_Path & sBE_DB
                Tdf.RefreshLink 'Refresh the link
            End If[/COLOR]

change the connect string and refresh the link

this assumes the backend connected table has the same name.
 

ecawilkinson

Registered User.
Local time
Today, 02:05
Joined
Jan 25, 2005
Messages
112
Yeah, Gemma's right. You can set the connected tablename using the SourceTablename property of the tabledef. What is important, is to call the RefreshLink method of the tabledef if you change any part of the its link information.

Chris
 

Mitch_____W

Mitch
Local time
Yesterday, 21:05
Joined
Oct 31, 2001
Messages
73
The .RefreshLink was what I was looking for. Thanks!

Got it working, however when the link refreshes, it is fairly time consuming considering I am dealing with ODBC connections. I think I will try to delete/append the TableDef with the modified .Connect property and see if that transpires faster.

I guess I should explain what I am doing:

I have many users who use many .mdb's each with many Linked ODBC connected tables. Most of these Tables are all accessed through the user's same Oracle DSN connection. The problem comes about every three months when the external Oracle database system requires users to change their passwords. After the user changes his/her password on the external Oracle database system, he/she must then delete and relink EACH of the numerous linked ODBC connected tables with numerous .mdb's they utilize.

I created this .mdb so the users can select (and store) all the .mdb's they use that have Linked ODBC connected tables to the respective Oracle database system. Then, when they are forced to change their password on the Oracle database system, all they have to do is open up this .mdb and enter their old password (to verify) and their new password and all of the connection strings are updated with their new passwords in every ODBC connected table in every .mdb they had previously selected (stored).

The .mdb is attached...

With a lil tweaking, I think someone might be able to use it as well...
 

Attachments

  • Change ODBC Table Connection Pwords.zip
    109 KB · Views: 440

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 02:05
Joined
Sep 12, 2006
Messages
15,660
the code given wont work with odbc connections, i dont think

with jet connectionds the string looks like

;database={path}

with odbc it looks different
 

Users who are viewing this thread

Top Bottom