Solved

Change table links in VBA

Posted on 2004-04-13
5
934 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

707 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

14 Experts available now in Live!

Get 1:1 Help Now