I need to be able to provide a method to update the server name in all ODBC linked table connections in an Access database. All tables have been migrated out of Access to a SQL Express instance. An option is needed to update all external table links to point from "Localhost\SQLExpress" to a SQL instance on another server. The database name will remain consistent. Only the Server instance name needs to be updated.
I've found examples of how to do this for connections to Access database files and Excel files, but not ODBC connections to SQL Server. One post here pointed out the need to dimension a db object and use it directly instead of trying to use CurrentDb directly. That got me further, but now the code fails with a type conversion when trying to assign the new connection string to the TableDef.
Dim OldServer As String
Dim NewServer As String
Dim OldPath As String
Dim NewPath As String
Dim strPath As String
NewServer = Me.NewServerInstance ' get new Server Instance name from form
OldPath = GetCurrentPath("Version")
'Parse old name from the ODBC connection string
OldServer = Replace(Left(OldPath, InStr(GetCurrentPath("Version"), "UID=") - 2), "ODBC Driver 13 for SQL Server;SERVER=", "")
NewPath = Replace(OldPath, OldServer, NewServer)
If NewServer = OldServer Then
GoTo UpdateInstance_Click_Exit
Else
'update all table connection strings.
'Loop & replace Old server instance with New server instance
Dim Db As DAO.Database
Set Db = CurrentDb
Dim td As DAO.TableDef
For Each td In Db.TableDefs
If (td.Attributes And dbAttachedODBC) = dbAttachedODBC Then
Db.TableDefs(td).Connect = NewPath 'getting a datatype conversion error here...
Db.TableDefs(td).RefreshLink
' MsgBox (db.TableDefs(td).Connect)
End If
Next
End If
The code example is what I came up with. There is a comment indicating the point where a datatype conversion error occurs. I guess I need to know if this is this even possible, or if I am trying to do something that is not possible, or just going about it the wrong way...