1

MS Accessではテーブルのリンクに絶対アドレス指定のみが許可されており、この問題の唯一の回避策はVBAコードを使用することであることがわかった後、そのための方法をコーディングし始めました。私は比較的単純なコードを見つけ、以下に示すように私の目的に合うように変更しました。ただし、この方法には2つの主な問題があるようです。

1-最初の試行でモジュール全体が破損するため、ExcelSpreedsheetsをリンクできないようです。それらをリンクする方法もありますか?

2-さらに重要なことに、ファイルを開くたびにファイルのサイズが大きくなり、データベースへの唯一の変更は、モジュール内のコードの追加です。ファイルを開くと自動的に実行されるように作成しました。閉じた後、サイズが数百kb増加することに気付きました。これは気がかりです。

また、これを行うためのより良い方法があれば、それがどのように行われるかを確認することに非常に興味があります。

Public Sub RelinkTables(newPathName As String, backEnd As String, excel1 As String, excel2 As String)
Dim Dbs As Database
Dim Tdf As TableDef
Dim Tdfs As TableDefs
Set Dbs = CurrentDb
Set Tdfs = Dbs.TableDefs
'Loop through the tables collection
   For Each Tdf In Tdfs
    If Tdf.SourceTableName <> "" Then 'If the table source is other than a base table
        If Tdf.SourceTableName = "CClas$" Or Tdf.SourceTableName = "Sheet1$" Then

        Else
            Tdf.Connect = ";DATABASE=" & newPathName & backEnd 'Set the new source
            Tdf.RefreshLink 'Refresh the link
        End If
    End If
Next 'Goto next table

End Sub

Function ReLinker()
Dim currPath As String
Dim backEnd As String
Dim excel1 As String
Dim excel2 As String
currPath = CurrentProject.Path
Debug.Print currPath
backEnd = "\backEnd.accdb"
excel1 = "\excel1.xls"
excel2 = "\excel2.xls"

RelinkTables currPath, backEnd, excel1, excel2
End Function
4

1 に答える 1