【问题标题】:Trying to link table between Access DBs using VBA. Getting ISAM not found error尝试使用 VBA 在 Access DB 之间链接表。获取 ISAM 未找到错误
【发布时间】:2016-08-15 17:00:09
【问题描述】:

我有一个拆分数据库,其中前端和后端都是 accdb 文件。因为我的一张表使用了AppendOnly = Yes 属性,所以当我移动后端时,我无法使用链接表管理器或refreshlink 属性。后端不时移动,因为我的 IT 喜欢重新调整服务器。

所以我的解决方案是编写一个提示后端位置的函数,删除所有当前链接的表,然后循环遍历所有后端表并将它们链接到前端。在最后一部分,我收到运行时错误 3170 找不到合适的 ISAM。我不知道为什么。

代码如下:

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

错误发生在

db.TableDefs.Append newtdf 

线。我正在寻找使此代码工作的方法,或者解决在使用 AppendOnly=Yes 属性时阻止刷新链接的已知错误的方法。

提前感谢您的帮助。

【问题讨论】:

  • 我正在处理的代码目前在我的桌面上进行开发。所以结果是: Database = \Users[My UserName]\Desktop\Database project files\BE\ACCLOGWINGSAU_be.accdb
  • winghei 发现了问题。但正如您预测的那样,我正在尝试链接我不知道存在的系统表。我将通过在删除它们之前将表名存储在一个数组中来解决这个问题,然后在我创建链接时循环它们,以便我只链接我需要的特定表。感谢您的帮助。

标签: vba ms-access linked-tables isam


【解决方案1】:

我认为您只是缺少字符串上的分号并删除了多余的空格

newtdf.Connect = ";Database=" & NetworkPath

【讨论】:

  • 我试过了。添加分号会导致 3001 无效参数错误。不过谢谢你的回复。
  • 去掉空格 ";Databases=" & .应该没问题,因为我在有空格时遇到了同样的错误
【解决方案2】:

或者,您可以使用DoCmd.TransferDatabase 方法并确保省略 MSys 表,因为它们在拆分文件之间没有直接的应用程序使用:

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

【讨论】:

    【解决方案3】:

    找到这个并为我工作

    DAODataSet.SQL.Text := 'SELECT * FROM Country IN "" ";DATABASE=C:\SIMPLE.MDB;PWD=MyPassword"';
    DAODataSet.Open;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      相关资源
      最近更新 更多