Solved

Change table links in VBA

Posted on 2004-04-13
5
954 Views
Last Modified: 2008-03-17
Hello Experts,

I would like to link tables to a backend database "on the fly" when a user selects an option or pushes a button etc.

How would I go about deleting the current table links and re-linking them to a new network path? The reason I am doing this is that I would like to have 1 tool that can work in any region, whereas I currently have 3 tools, 1 for each region.

Thanks in advance for your help!
0
Comment
Question by:sph3rion
5 Comments
 
LVL 11

Expert Comment

by:phileoca
ID: 10814388
what we do at our company is use Map Network drives.  We picked S, so all of our links go to the S drive, so no matter where we go, to connect, to the backend (which we have 2 duplicates at 2 satellite locations, 1 one via the web server) connecting to the S drive connects it to the backend

However, you can Dynamically create links, however you'll have to specify the path and name before hand... which really doesn't make it "on the fly".

<>< KT
0
 
LVL 1

Author Comment

by:sph3rion
ID: 10814813
I can specify the path and name in VBA right?

The problem is that someone set up 3 different databases with identical tables (names and format) with autonumbers and the like as primary and foreign keys, it's not a design I would have come up with but I must maintain it. So since I can't merge any of the data into one database, I need the tool to function as if it were.

I know it can be done as I have seen code that looks like it can be made to work in this way but I need some help with it.
0
 
LVL 54

Expert Comment

by:nico5038
ID: 10814955
Personally I use this function to relink to the _be.mdb in the same directory as the frontend:

Function fncRelink()
'functie om de gelinkte tabellen te verversen naar de "_be" database
'Hierbij wordt aangenomen dat de "_be" database in dezelfde map staat
'als de frontend

Dim td As TableDef

For Each td In CurrentDb.TableDefs
  If Len(td.Connect) > 0 Then
     td.Connect = ";DATABASE=" & Left(CurrentDb.Name, Len(CurrentDb.Name) - 4) & "_be.mdb"
     td.RefreshLink
  End If
Next


End Function

But I guess it gives you an indication :-)

Nic;o)
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 10814965
I have some code which I use as we use one frontend but different versions of the same database

I created this, pass in name of DB, password if any, TRUE to create links, FALSE otherwise


Public Function Install_RefreshLinks(ByVal sDB As String, ByVal sPassword As String, ByVal bCreateLinks As Boolean) As Boolean
   
    Dim dbData As Database
    Dim tdData As TableDef
    Dim sTmp As String
    Dim i As Integer
    Dim bLoop As Boolean
    Dim iAbortLoop As Integer
    Dim bDone As Boolean
    Dim iCnt As Integer
 
 
    On Error GoTo ins_err
   
    Install_RefreshLinks = False
   
'Using refreshlink keeps failing with installable isam error,
'To avoid this on other client machines, Im cheating by deleting first

    Err.clear
    iAbortLoop = 0
   
    If bCreateLinks = False Then
        Set dbData = CurrentDb
        bLoop = True
        iCnt = 0
        While bLoop = True
            bDone = False
            For Each tdData In dbData.TableDefs
                If tdData.Attributes = dbAttachedTable Then
                      dbData.TableDefs.Delete tdData.Name
                      iCnt = iCnt + 1
                    bDone = True
                End If
            Next tdData
            iAbortLoop = iAbortLoop + 1
            If bDone = False Or iAbortLoop = 500 Then
                bLoop = False
                MsgBox iCnt & " linked tables were removed.", vbInformation, APP_TITLE
            End If
        Wend
        'Now recreate links
        'If Err.Number = 0 Then bCreateLinks = True
    End If
 
    If bCreateLinks = True Then
        iCnt = 0
        If sPassword <> "" Then
            Set dbData = DBEngine.Workspaces(0).openDatabase(sDB, False, False, ";PWD=" & sPassword)
        Else
            Set dbData = DBEngine.Workspaces(0).openDatabase(sDB)
        End If
   
        For Each tdData In dbData.TableDefs
            If tdData.Attributes = 0 Then
                If tdData.Name <> "RPT_Temp" And tdData.Name <> "MainMenu" Then
                    'Cater for temp tables used by Dev but not for release
                    If Left$(tdData.Name, 3) <> "xx_" Then
                        DoCmd.TransferDatabase acLink, "Microsoft Access", sDB, acTable, tdData.Name, tdData.Name, False
                        iCnt = iCnt + 1
                    End If
                End If
            End If
        Next
   
        MsgBox iCnt & " linked tables were created.", vbInformation, APP_TITLE
    End If

    fCaller.stbEMEA.Panels(1).Text = ""
    Install_RefreshLinks = True
    GoTo ins_ok
   
ins_err:
    MsgBox "Error " & Err.Number & " trapped. " & vbCrLf & Err.Description, vbCritical, APP_TITLE

ins_ok:
    Set dbData = Nothing
End Function

0
 
LVL 1

Author Comment

by:sph3rion
ID: 10815103
Thanks very much!

I ran into some work but I will try out this code in a few hours
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

813 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

18 Experts available now in Live!

Get 1:1 Help Now