2016-08-15 3 views
0

J'ai une base de données partagée où l'extrémité avant et l'extrémité arrière sont des fichiers accdb. Parce que l'une de mes tables utilise la propriété AppendOnly = Yes, je ne peux pas utiliser le gestionnaire de table de liens ou la propriété refreshlink lorsque je déplace le backend. Le backend se déplace de temps en temps parce que mon département informatique aime réorganiser les serveurs. Donc, ma solution est d'écrire une fonction qui demande l'emplacement du backend, supprime toutes les tables actuellement liées, puis boucle toutes les tables backend et les relie au frontend. Sur cette dernière partie, je reçois une erreur d'exécution 3170 n'a pas pu trouver ISAM approprié. Je ne sais pas pourquoi.Essayer de lier une table entre des DB d'accès en utilisant VBA. Obtention de l'erreur ISAM introuvable

code

est ci-dessous:

Public Function MoveDB() 

'this function will replace the linked table manager. It will open a file select dialog box to allow the user to pick the new location of the DB backend. 
'It will then break all the current links and then recreate them. We need to do this vice use the relink function because the cases table uses AutoAppend which stores old path data 
' and breaks the relink function which is why linked table manager does not work. 

' FileDialog Requires a reference to Microsoft Office 11.0 Object Library. 

'variables to get the database path 
Dim fDialog As Office.FileDialog 
Dim varFile As Variant 
Dim DriveLetter As String 
Dim NetworkPath As String 
Dim DrivePath As String 
Dim SubPath As String 


'variables to link the database 
Dim db As DAO.Database 
Dim BEdb As DAO.Database 
Dim oldtdf As DAO.TableDef 
Dim tblName As String 
Dim newtdf As DAO.TableDef 
Dim BEtdf As DAO.TableDef 


Set db = CurrentDb() 

' Set up the File Dialog. 
Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 
With fDialog 

    ' Do not Allow user to make multiple selections in dialog box 
    .AllowMultiSelect = False 

    'set the default folder that is opened 
    .InitialFileName = CurrentProject.Path & "\BE" 

    ' Set the title of the dialog box. 
    .Title = "Please select the Database Backend" 

    ' Clear out the current filters, and add our own. 
    .Filters.Clear 
    .Filters.Add "Access Databases", "*.accdb" 

    ' Show the dialog box. If the .Show method returns True, the 
    ' user picked a file. If the .Show method returns 
    ' False, the user clicked Cancel. 
    If .Show = True Then 

'We need to determine the full network path (including server name) to the DB backend. The reason is that different users may have the share drive mapped with different letters. 
'If the backend is mapped using the drive letter of the user moving the DB then other users may not have a valid path. The full network path is universal 


'Get the mapped drive letter from the path of the selected DB file 
    DriveLetter = Left$(Trim(fDialog.SelectedItems(1)), 2) 
'Get the path of the selected DB file minus the drive letter 
    SubPath = Mid$(Trim(fDialog.SelectedItems(1)), 3) 
'Get the full network path of the mapped drive letter 
    DrivePath = GETNETWORKPATH(DriveLetter) 
'Combine the drive path and the sub path to get the full path to the selected DB file 
    NetworkPath = DrivePath & SubPath 
    'MsgBox (NetworkPath) 
    Else 
    MsgBox "You clicked Cancel in the file dialog box." 
    End If 
End With 
    'Now we need to delete all the linked tables 

For Each oldtdf In db.TableDefs 
    With oldtdf 
     If oldtdf.Attributes And dbAttachedODBC Or oldtdf.Attributes And dbAttachedTable Then 
     'this is a linked table 
      tblName = .Name 
      DoCmd.DeleteObject acTable, tblName 
     End If 
    End With 
Next oldtdf 
tblName = "" 


'Now we link all the tables from the backend to the front end 
Set BEdb = OpenDatabase(NetworkPath) 
For Each BEtdf In BEdb.TableDefs 
    tblName = BEtdf.Name 
    If Left(tblName, 4) <> "~TMP" Then 
     Set newtdf = db.CreateTableDef(strTable) 
     newtdf.Connect = "Database = " & NetworkPath 
     newtdf.SourceTableName = tblName 
     newtdf.Name = tblName 
    db.TableDefs.Append newtdf 
    End If 
Next BEtdf 

End Function 

L'erreur se produit sur la ligne

db.TableDefs.Append newtdf 

. Je cherche soit à faire fonctionner ce code, ou un moyen de contourner le bogue connu qui empêche l'actualisation des liens lors de l'utilisation de la propriété AppendOnly=Yes.

Merci d'avance pour toute aide.

+0

peuvent être ce sera utile: https://support.microsoft.com/en-us/kb/209805 – cyboashu

+0

Le code je travaille actuellement sur mon bureau pour le développement. le résultat est donc: Database = \ Users \ [Mon nom d'utilisateur] \ Desktop \ Fichiers de projet de base de données \ BE \ ACCLOGWINGSAU_be.accdb –

+0

winghei a trouvé le problème. Mais comme vous l'avez prédit, j'essaie de relier des tables système que je ne connaissais pas. Je vais corriger cela en stockant les noms des tables dans un tableau avant de les supprimer et de les parcourir ensuite lorsque je créerai les liens de sorte que je ne lierai que les tableaux spécifiques dont j'ai besoin. Merci pour l'aide. –

Répondre

1

Je pense que vous manquez juste le point-virgule sur votre chaîne et de supprimer des espaces supplémentaires

newtdf.Connect = ";Database=" & NetworkPath 
+0

J'ai essayé ça. L'ajout du point-virgule provoque une erreur d'argument 3001 non valide. Mais merci pour la réponse. –

+0

supprime les espaces "; Databases =" &. Ca devrait aller puisque j'ai eu la même erreur que quand il y a des espaces – winghei

+1

Ça a marché. merci –

0

Vous pouvez également utiliser la méthode DoCmd.TransferDatabase et assurez-vous de laisser les tables MSys car ils ne servent à rien d'application directe entre diviser des fichiers:

If Left(tblName, 4) <> "~TMP" And Left(tblName, 4) <> "MSys" Then 
    DoCmd.TransferDatabase acLink, "Microsoft Access", NetworkPath, _ 
          acTable, tblName, tblName, False 
End If 
+0

Bon conseil. Merci –