【问题标题】:Access VBA - Relink external excel workbookAccess VBA - 重新链接外部 Excel 工作簿
【发布时间】:2018-02-03 03:00:11
【问题描述】:

如何让 MS Access 通过 VBA 宏重新链接外部 Excel 工作簿?

我可以使用链接表管理器执行此操作,但我想通过 VBA 执行此操作,以便我可以创建一个按钮供用户按下以查找新工作簿

  1. 选择新工作簿
  2. 重新链接外部 Excel 工作簿

DoCmd.transferSpreadsheet aclink,,"Sales", "C:\Sales.xlsb", true, "Sales!E2:BC200"

【问题讨论】:

    标签: ms-access vba ms-access-2010


    【解决方案1】:

    我使用以下代码重新连接到链接表。

    Public Function FixTableLink()
    
    Dim db As Database
    Dim strPath As String
    Dim strConnect As String
    
    strPath = CurrentProject.Path
    strPath = strPath & "\DatabaseName.extention"
    
    strConnect = ";DATABASE=" & strPath
    
    Set db = CurrentDb
    
    For Each tbl In db.TableDefs
    
        If Nz(DLookup("Type", "MSysObjects", "Name = '" & tbl.name & "'"), 0) = 6 And tbl.Connect <> strConnect Then
    
            tbl.Connect = strConnect
            tbl.RefreshLink
    
        End If
    
    Next tbl
    
    End Function  
    

    把strPath改成你后端的路径

    您可以使用以下代码打开一个对话框来搜索文件路径

    Function SelectFile() As String
    
            On Error GoTo ExitSelectFile
    
            Dim objFileDialog    As Object
            Set objFileDialog = Application.FileDialog(1)
    
            With objFileDialog
    
                .AllowMultiSelect = False
                .Show
    
                Dim varSelectedItem As Variant
    
                For Each varSelectedItem In .SelectedItems
    
                    SelectFile = varSelectedItem
    
                Next varSelectedItem
    
           End With
    
        ExitSelectFile:
    
        Set objFileDialog = Nothing
    
    End Function
    
    'File type filters can be added to the filedialog property using the following syntax:
    
       '.Filters.Clear
       '.Filters.Add "File Type Description", "*.file extension"
    
    ''Start folder can be specified using:
    '.initialfilename="folder path"
    

    那么在第一个代码块中就可以使用了

    strPath =选择文件

    【讨论】:

      【解决方案2】:

      也许是这样的。

      Dim InputFile As String
      Dim InputPath As String
      
      InputPath = "C:\ExcelPath\"
      InputFile = Dir(InputPath & "*.xls")
      
      Do While InputFile <> ""
      
          DoCmd.TransferSpreadsheet acLink, , "Your table name","Path to your workbook file", True, "Sheet1!RangeYouNeed"
          InputFile = Dir
      Loop
      

      【讨论】:

        猜你喜欢
        • 2015-08-06
        • 1970-01-01
        • 1970-01-01
        • 2023-01-14
        • 2018-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多