Change Linked tables to another database path (1 Viewer)

JayAndy

Registered User.
Local time
Today, 22:03
Joined
Jan 13, 2016
Messages
31
Hi All

I am trying to change the path of linked tables for my database for training process for dummy data.

Whats the easiest way to change the path of all my tables to a dummy database. The dummy database will have the same table names inside it it will just be the path l need to change.

Thanks

J
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 22:03
Joined
Sep 12, 2006
Messages
15,651
You can do it manually. (table relinker/linked table manager)

what I do is use code to drop all the linked tables, and then re-connect them to the new location.
(I actually maintain a table of the tables that need to be linked!)

Alternatively, you can just update the connection part and refresh the links, but I don't do that.

I can't post code at the moment, but just search for table relinking, or backend autoupdate, and you will find the various AWF solutions.
 

JayAndy

Registered User.
Local time
Today, 22:03
Joined
Jan 13, 2016
Messages
31
Thanks looking to do it in VBa code not manually. will search and have a look.
 

Tieval

Still Clueless
Local time
Today, 22:03
Joined
Jun 26, 2015
Messages
475
A text box and two buttons in a form can do this:

Code:
Private Sub Select_Click()
'Requires reference to Microsoft Office 12.0 Object Library.
   Dim fDialog As Office.FileDialog
   Dim varFile As Variant
   'Set up the File Dialog.
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
   With fDialog
    'Allow user to make multiple selections in dialog box.
    .AllowMultiSelect = True
    'Set the title of the dialog box.
    .Title = "Choose Database Network Location"
    'Clear out the current filters, and add our own.
    .Filters.Clear
    .Filters.Add "Database", "*.accdb"
    .Filters.Add "All Files", "*.*"
    'Show the dialog box. If the .Show method returns True, the
    'user picked at least one file. If the .Show method returns
    'False, the user clicked Cancel.
    If .Show = True Then
    'Loop through each file selected and add it to the list box.
    For Each varFile In .SelectedItems
    Me.FileList.AddItem varFile
    Next
    Else
    MsgBox "You clicked Cancel in the file dialog box."
    End If
    End With
End Sub

Private Sub Update_Click()
Me.FileList.SetFocus
Me.FileList.Selected(0) = True
'If file name has been selected (Default value is 1 so must be longer to show selection)
If Len(Me.FileList.Value) > 1 Then
'Change datapath to selected network drive
Dim tdef As TableDef
Dim Strnew As String
Strnew = ";database=" & Me.FileList
For Each tdef In CurrentDb.TableDefs
If Left(tdef.Connect, Len(";Database=")) = ";database=" Then
If Not InStr(tdef.Connect, "mydb.accdb") Then
Debug.Print tdef.Name; tdef.Connect
tdef.Connect = Strnew
tdef.RefreshLink
End If
End If
Next
End If
'Close form and refresh main form
DoCmd.Close
DoCmd.RunMacro "Refresh"
End Sub
 

JayAndy

Registered User.
Local time
Today, 22:03
Joined
Jan 13, 2016
Messages
31
I would like to do this without using forms if possible.

Got this code off the internet but when l use it It gives me a runtime error 3001: Invalid agreement.

Any Help why?

Sub Change_Links()

Set db = CurrentDb
Dim fso As New FileSystemObject
Dim fileName As String
Dim dbPath As String

dbPath = "C:\Users\user\Desktop\Develpoment\Dummy Training"

For Each tdf In db.TableDefs

If tdf.Connect Like ";DATABASE*" Then

fileName = fso.GetFileName(tdf.Connect)
tdf.Connect = ";DATABASE" & dbPath & "\" & fileName
tdf.RefreshLink
End If

Next tdf


End Sub
 

Tieval

Still Clueless
Local time
Today, 22:03
Joined
Jun 26, 2015
Messages
475
A total guess here but try using something other than a user directory, I am always suspicious of c:\users as it can sometimes be a windows manufactured thing rather than an absolute path.

Try putting it all in c:\test or something like that
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 22:03
Joined
Sep 12, 2006
Messages
15,651
it will be

dbPath = "C:\Users\user\Desktop\Develpoment\Dummy Training"


I am sure it will be looking for a username in the red part of the above string.
check the folders you have. you will probably see an "allusers", and then individual folders for each user login you have created. General a user does not have privileges over another users folder, but it depends how the users are set up. An "admin" user probably does have privileges.
 

Users who are viewing this thread

Top Bottom