我有几个表通过Microsoft Query连接到访问数据库。如果我移动访问文件的位置或者我需要一种更新源位置的方法,特别是因为我需要与其他人共享此文件。
所有连接都是ODBC,来自同一个访问文件。
答案 0 :(得分:0)
由于所有连接都是统一的,我遍历每个连接并用当前通过Windows文件浏览器选择的文件替换当前源文件。
Sub SwitchODBCSource()
Dim conn As WorkbookConnection
Dim sOldConnection As String, sNewConnection As String
getfilePath = Application.GetOpenFilename()
FileType = ".accdb"
If InStr(getfilePath, FileType) Then
fileName = Dir(getfilePath)
filePath = Replace(getfilePath, "\" & fileName, "")
For Each conn In ActiveWorkbook.Connections
With conn
conn.ODBCConnection.BackgroundQuery = False
conn.ODBCConnection.CommandType = xlCmdSql
conn.ODBCConnection.Connection = Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=" & filePath & "\" & fileName & ";DefaultDir=" _
), Array( _
filePath & ";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _
))
End With
Next conn
ActiveWorkbook.RefreshAll
Call Sheet1.dropDown
Set conn = Nothing
Else
MsgBox ("Can only use " & FileType & " files")
End If
End Sub