I have already referenced other pages for my problem but I still can't get this to work. I feel a bit slow given that I have three examples below and still can't figure this out.
Changing linked table location programatically
Linked table ms access 2010 change connection string
Update an Access linked table to use a UNC path
Here is the code that I am using:
Dim tdf As TableDef
Dim db As Database
Set db = CurrentDb
Set tdf = db.TableDefs("DeviceListT")
tdf.Connect = "ODBC;DATABASE=" & CurrentProject.path _
& "\HarmonicProfileDatabase_be.accdb"
tdf.RefreshLink
The problem is that when I run it a window pops up.
I am not exactly sure what I am supposed to do with that nor do I want it to pop up in the first place as I will be giving the ms access files to someone else and they won't know what to do with this window either.
You are using SQL Server references but linking MS Access. For MS Access, you do not need an ODBC link, just refer to DATABASE:
DBFile = CurrentProject.path & "\HarmonicProfileDatabase_be.accdb
''Check the file exists
strFile = Dir(DBFile)
If strFile <> "" Then
With CurrentDb
For Each tdf In .TableDefs
''Check that this is a linked table
''It can be useful to use table of tables instead
If tdf.Connect Like "*HarmonicProfileDatabase_be.accdb*" Then
tdf.Connect = ";DATABASE=" & DBFile
tdf.RefreshLink
End If
Next
End With
MsgBox "Link HarmonicProfileDatabase_be.accdb"
Else
MsgBox "Problem"
End If
You could also use:
sConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
& DBFile & ";Jet OLEDB:Database Password=pw;"
Related
I have the following procedure that I run (in an MS Access module) on a regular basis to switch the linked table connection strings from Test to Production and vice-versa:
Public Function TableRelink()
Dim db As Database
Dim strConnect As String
Dim rs As Recordset
Dim tdf As TableDef
strConnect = "ODBC;DRIVER=SQL Server;SERVER=MYTESTSERVER;Trusted_Connection=Yes;APP=Microsoft Office 2013;DATABASE=MyDB;"
'strConnect = "ODBC;DRIVER=SQL Server;SERVER=MYLIVESERVER;Trusted_Connection=Yes;APP=Microsoft Office 2013;DATABASE=MyDB;"
CurrentDb.TableDefs.Refresh
For Each tdf In CurrentDb.TableDefs
If tdf.Connect <> "" Then
tdf.Connect = strConnect
tdf.RefreshLink
End If
Next
MsgBox ("Done!")
End Function
The above has been working for months and months. About a week ago, the following error randomly popped up. Then, after a few minutes, without any intervention on my part, it would allow me to run the procedure again. Today, the error has come back.
Run-time error '3035': System resource exceeded.
It is thrown on this line: tdf.RefreshLink
I did a Google search and found an article out there talking about a hotfix (that wouldn't install on my machine), and another about editing a registry value (which didn't seem fix it). As I type this, the error has stopped popping up and I can again re-link my tables, so at this point, I can't really do any more troubleshooting. I was reading another SO post talking about the lock file, but couldn't really make heads or tails of the accepted answer, and I'm not really convinced it has anything to do with my particular scenario. Does anyone know what might be causing this and/or what can be done to prevent it?
For reference, I'm running Office 365 ProPlus on a Win10 64-bit machine.
First, do use your db object:
Public Function TableRelink()
Dim db As Database
Dim strConnect As String
Dim rs As Recordset
Dim tdf As TableDef
strConnect = "ODBC;DRIVER=SQL Server;SERVER=MYTESTSERVER;Trusted_Connection=Yes;APP=Microsoft Office 2013;DATABASE=MyDB;"
'strConnect = "ODBC;DRIVER=SQL Server;SERVER=MYLIVESERVER;Trusted_Connection=Yes;APP=Microsoft Office 2013;DATABASE=MyDB;"
Set db = CurrentDb
db.TableDefs.Refresh
For Each tdf In db.TableDefs
If tdf.Connect <> "" Then
tdf.Connect = strConnect
tdf.RefreshLink
End If
Next
MsgBox ("Done!")
Set td = Nothing
Set db = Nothing
End Function
However, another and a much faster method is to have both sets of tables linked permanently and then rename them for switching the database.
For example, to switch from Production to Test:
Table1 -> Table1_p
Table2 -> Table2_p
...
Table1_t -> Table1
Table2_t -> Table2
Of course, if you modify a table schema, you must relink as usual.
How can I link a table from one MS Access Database (*.mdb or *.accdb) to another Access database in VBA?
Basically I just was to use VBA to replicate what the External Data wizard does.
I've googled this and I see many examples of how to update or relink tables and many examples of linking to SQL databases, but very few of simple linking tables between Access databases.
You can use the DoCmd.TransferDatabase Method to create a link to a table in another Access database.
DoCmd.TransferDatabase TransferType:=acLink, _
DatabaseType:="Microsoft Access", _
DatabaseName:="C:\share\Access\Example Database.accdb", _
ObjectType:=acTable, _
Source:="Addresses", _
Destination:="Addresses_link"
I included the option names hoping that would make it easier to track which option is which. But if that seems too verbose, you can omit the option names and do it all on one line:
DoCmd.TransferDatabase acLink, "Microsoft Access", "C:\share\Access\Example Database.accdb", acTable , "Addresses", "Addresses_link"
It's actually pretty easy--you just create a new tabledef and set its .connect property to an ODBC connection string that links to the other Access database.
Private Function LinkTable(LinkedTableName As String, TableToLink As String, connectString As String) As Boolean
Dim tdf As New dao.TableDef
On Error GoTo LinkTable_Error
With CurrentDb
.TableDefs.Refresh
Set tdf = .CreateTableDef(LinkedTableName)
tdf.Connect = connectString
tdf.SourceTableName = TableToLink
.TableDefs.Append tdf
.TableDefs.Refresh
End With
Set tdf = Nothing
End Function
The connection string would look something like this (taken from connectionstrings.com):
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Persist Security Info=False;
DataWriter, are you not mixing DAO with ADO. That's like water & oil. The connectionstring is OLEDB (which is ADO) and TableDef is from CurrentDb (which is DAO).
The Connect Property of a TableDef is in the form of:
;DATABASE=[Full path to Db]\[Db Name]
I'm using the below code to create a DNS-Less connection to an Oracle DB and this code works fine. Some information has been removed due to its sensitive nature. Again the posted code is working. I'm looking to add the ability to make a table within access. How do I take the query results and place them into a table?
Dim tdf As TableDef
Dim DB As Database
Set DB = CurrentDb
'Rem create link to groups def
Set tdf = DB.CreateTableDef("Sale")
tdf.Connect = "ODBC;Driver={Microsoft ODBC for Oracle};Server=" & ServerName & ";UID=" & UID & ";Pwd=" & PWD & ""
tdf.SourceTableName = "SBOMAST_OWN.SALE"
DB.TableDefs.Append tdf
DB.Close
Set DB = Nothing
Assuming you want to make the table from scratch, you will want to use a make table query, which in VBA, takes the form of SELECT * INTO Target FROM Source. I know with ODBC, you can put the connection string directly into the Target and Source values, eliminating the need for a recordset. Your query would become:
"SELECT * INTO NewAccessTable FROM (" & strConnectionString & ") OracleDBTable"
Check out this article for more information: http://support.microsoft.com/kb/200427
Hey.
I have the main access database located on a stand alone PC off the network and i have a access database with linked tables on the network linked back to the stand alone PC. I have linked the tables by creating a network share to the stand alone PC and linking them though a path. Can i set it up so that when the database is opened it automatically updates the linked tables.
Ben
You can. I often find it convenient to use a small check form that runs on start-up (set through start-up options) and checks a variety of things, including linked tables. To this end, I also hold a table of linked tables on the local machine, although a list of linked tables can be obtained by iterating through the TableDefs collection, I think it is slightly safer to keep a list.
The check form can check all links and if a link is broken or missing, either ask the user for a new location or use a fixed location. If no problems are found, the form can close itself and open a menu or other form.
In the case of linking to a linked table, it is possible to get the connection to use from:
CurrentDB.TableDefs("TableName").Connection
Here are some more notes:
Sub RelinkTables(Optional strConnect As String = "")
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL
Dim tdf As DAO.TableDef
On Error GoTo TrapError
Set db = CurrentDb
If strConnect = "" Then
''Where Me.txtNewDataDirectory is a control on the check form
strConnect = "MS Access;PWD=databasepassword;DATABASE=" & Me.txtNewDataDirectory
End If
''Table of tables to be linked with two fields TableName, TableType
Set rs = CurrentDb.OpenRecordset("Select TableName From sysTables " _
& "WHERE TableType = 'LINK'")
Do While Not RS.EOF
''Check if the table is missing
If IsNull(DLookup("[Name]", "MSysObjects", "[Name]='" & rs!TableName & "'")) Then
Set tdf = db.CreateTableDef(RS!TableName, dbAttachSavePWD, _
rs!TableName, strConnect)
''If the table is missing, append it
db.TableDefs.Append tdf
Else
''If it exists, update the connection
db.TableDefs(rs!TableName).Connect = strConnect
End If
db.TableDefs(rs!TableName).RefreshLink
RS.MoveNext
Loop
Set db = Nothing
RS.Close
Set RS = Nothing
Exit_Sub:
Exit Sub
TrapError:
HandleErr Err.Number, Err.Description, "Relink Tables"
End Sub
I can create an Access mdb and add a linked table to an Sql Server database via ODBC. If I change the Sql Server that the ODBC is connecting to with the ODBC control panel applet the mdb still connects to the original Sql Server until Access is restarted.
Is there a way to relink these linked server tables without restarting Access?
EDIT: I would like to do this in code
You can use the code below to refresh all ODBC tables in your Access project to a given DSN.
How to use it
Just copy the code in a new or existing VBA module and, where you want to refresh the links, call it with the proper DSN for the new ODBC connection:
RefreshODBCLinks "ODBC;DRIVER=SQL Server Native Client 10.0;" & _"
"SERVER=SQLSERVER;UID=Administrator;" & _
"Trusted_Connection=Yes;" & _
"APP=2007 Microsoft Office system;DATABASE=OrderSystem;"
Also, have a look at the Access help for the TableDef.RefreshLink method.
Code version 1
Classic way of relinking but Access may keep connection information in memory if the tables have been used before RefreshODBCLinks is called.
Public Sub RefreshODBCLinks(newConnectionString As String)
Dim db As DAO.Database
Dim tb As DAO.TableDef
Set db = CurrentDb
For Each tb In db.TableDefs
If Left(tb.Connect, 4) = "ODBC" Then
tb.Connect = newConnectionString
tb.RefreshLink
Debug.Print "Refreshed ODBC table " & tb.Name
End If
Next tb
Set db = Nothing
End Sub
Code version 2
This will completely re-create the ODBC linked tables: the old ones will be renamed, then new tables using the given DSN will be created before deleting the old linked version.
Please make sure you test this and maybe add some code to better handle errors as necessary.
Note also that the parameter dbAttachSavePWD passed during creation of the ODBC table will save the ODBC password (if any) in Access. Just remove it if that's not what you need.
Public Sub RefreshODBCLinks(newConnectionString As String)
Dim db As DAO.Database
Dim tb As DAO.TableDef
Dim originalname As String
Dim tempname As String
Dim sourcename As String
Dim i As Integer
Set db = CurrentDb
' Get a list of all ODBC tables '
Dim tables As New Collection
For Each tb In db.TableDefs
If (Left(tb.Connect, 4) = "ODBC") Then
tables.Add Item:=tb.Name, key:=tb.Name
End If
Next tb
' Create new tables using the given DSN after moving the old ones '
For i = tables.count To 1 Step -1
originalname = tables(i)
tempname = "~" & originalname & "~"
sourcename = db.TableDefs(originalname).SourceTableName
' Create the replacement table '
db.TableDefs(originalname).Name = tempname
Set tb = db.CreateTableDef(originalname, dbAttachSavePWD, _
sourcename, newConnectionString)
db.TableDefs.Append tb
db.TableDefs.Refresh
' delete the old table '
DoCmd.DeleteObject acTable, tempname
db.TableDefs.Refresh
tables.Remove originalname
Debug.Print "Refreshed ODBC table " & originalname
Next i
Set db = Nothing
End Sub
One last thing: if you're still getting issues that require that you restart Access for the changes to be visible, then have a look at my code in Restarting and compacting the database programmatically on my site.
Note: Code Version 2 was inspired in part from this Access Web article.
What version of Access are you using? In 2000, you can go to Tools>Database Utilities>Linked Table Manager to change your settings.