Hi,
I have an access app which gets data from a SQL server. The server will be changing and I need to relink all the linked tables to the new SQL server. I found code in the web to do this but I'm having some trouble applying it.
Here's what I have:
Private Sub cmdLink11_Click()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim rst As DAO.Recordset
Dim strServer As String
Dim strDB As String
Dim strTable As String
Dim strConnect As String
Dim strMsg As String
On Error GoTo HandleErr
' Build base authentication strings
strConnect = "ODBC;DRIVER={sql server};DATABASE=xxx" & _
";SERVER=xxx" & _
";Trusted_Connection=Yes;"
' Create recordset to obtain server, database and table names
Set db = CurrentDb
Set rst = db.OpenRecordset("tblSQLTables", dbOpenSnapshot)
If rst.EOF Then
strMsg = "There are no tables listed in tblSQLTables."
End If
' Walk through the recordset and create the links
Do Until rst.EOF
strServer = rst!SQLServer
strDB = rst!SQLDatabase
strTable = rst!SQLTable
' Create a new TableDef object
Set tdf = db.CreateTableDef(strTable)
' Set the Connect property to establish the link
tdf.Connect = strConnect & "Server=" & strServer & ";Database=" & strDB & ";"
tdf.SourceTableName = strTable
' Append to the database's TableDefs collection
db.TableDefs.Append tdf
tdf.RefreshLink
'tdf.close
rst.MoveNext
Loop
strMsg = "Tables linked successfully."
rst.Close
Set rst = Nothing
Set tdf = Nothing
Set db = Nothing
ExitHere:
MsgBox strMsg, , "Link SQL Tables"
Exit Sub
HandleErr:
Select Case Err
Case Else
strMsg = Err & ": " & Err.Description
Resume ExitHere
End Select
End Sub
I have created an access table called: tblSQLTables and added the name of the table I want to relink in it. That linked table exists in my current access app.
The code runs until line "strServer = rst!SQLServer" then it jumps to: "Select Case Err" and I get the following error message: 3265: Item not found in this collection
Could someone help me understand the error. Is this a bad connection to the database or to the linked table?
Thank you for any input!
I have an access app which gets data from a SQL server. The server will be changing and I need to relink all the linked tables to the new SQL server. I found code in the web to do this but I'm having some trouble applying it.
Here's what I have:
Private Sub cmdLink11_Click()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim rst As DAO.Recordset
Dim strServer As String
Dim strDB As String
Dim strTable As String
Dim strConnect As String
Dim strMsg As String
On Error GoTo HandleErr
' Build base authentication strings
strConnect = "ODBC;DRIVER={sql server};DATABASE=xxx" & _
";SERVER=xxx" & _
";Trusted_Connection=Yes;"
' Create recordset to obtain server, database and table names
Set db = CurrentDb
Set rst = db.OpenRecordset("tblSQLTables", dbOpenSnapshot)
If rst.EOF Then
strMsg = "There are no tables listed in tblSQLTables."
End If
' Walk through the recordset and create the links
Do Until rst.EOF
strServer = rst!SQLServer
strDB = rst!SQLDatabase
strTable = rst!SQLTable
' Create a new TableDef object
Set tdf = db.CreateTableDef(strTable)
' Set the Connect property to establish the link
tdf.Connect = strConnect & "Server=" & strServer & ";Database=" & strDB & ";"
tdf.SourceTableName = strTable
' Append to the database's TableDefs collection
db.TableDefs.Append tdf
tdf.RefreshLink
'tdf.close
rst.MoveNext
Loop
strMsg = "Tables linked successfully."
rst.Close
Set rst = Nothing
Set tdf = Nothing
Set db = Nothing
ExitHere:
MsgBox strMsg, , "Link SQL Tables"
Exit Sub
HandleErr:
Select Case Err
Case Else
strMsg = Err & ": " & Err.Description
Resume ExitHere
End Select
End Sub
I have created an access table called: tblSQLTables and added the name of the table I want to relink in it. That linked table exists in my current access app.
The code runs until line "strServer = rst!SQLServer" then it jumps to: "Select Case Err" and I get the following error message: 3265: Item not found in this collection
Could someone help me understand the error. Is this a bad connection to the database or to the linked table?
Thank you for any input!