This table relinking procedure is a really good example of how to use VBA to link your tables programmatically, and how to have MS Access refresh linked tables automatically.
It allows re-linking all tables to a selected drive source (the folder the backend database is in).
In this example, all of the tables that need to be relinked are in the “LinkTables” table, and the backend database is in the “C:\Interface\20180314” folder:
IMPORTANT: All Backend databases must reside in the same path, and name of file listed in table. If the tables are not in the same backend database, then the code needs to be modified.
I am interacting with this procedure from a form that looks like this:
Here’s the code for the form:
Private Sub btnLinkTables_Click() RefreshLink "C", Me.txtBackendPath End Sub Private Sub Form_Load() Me.txtBackendPath = "C:\Interface\backend" End Sub
Here’s the code of how to call the “RefreshLink” procedure:
Option Compare Database Public Sub RefreshLink(BackendDrive, BackendPath) Dim strSourceDatabase As String Dim strSourceName As String Dim strLocalName As String Dim strSQL As String Dim strDBName As String Dim rs As Recordset Dim intPos As Integer Dim DriveLetter As String Dim FullPath As String Dim BlockName As String DoCmd.Hourglass False DriveLetter = BackendDrive '"\\jlp-fs01\SB2\" strSQL = "SELECT SourceTableName, DestinationTableName, TableGroup, DateAdded, BackendDatabase, BackendDatabasePath, NetworkOnly FROM LinkedTables" Set rst = CurrentDb.OpenRecordset(strSQL) rst.MoveFirst Do Until rst.EOF strSourceName = rst.Fields("SourceTableName") strLocalName = rst.Fields("DestinationTableName") strBlockName = rst.Fields("TableGroup") strDBName = rst.Fields("BackendDatabase") strSourceDatabase = BackendPath & "\" & strDBName If Relink(strSourceName, strSourceDatabase, strLocalName, strLocalName) = False Then Set rst = Nothing Exit Sub Else 'Provide user info on screen Forms![frmSystemConfiguration].lblStatus.caption = "Linking " & BlockName & ": " & strSourceName DoEvents End If rst.MoveNext Loop Set rst = Nothing 'Tell the user the relink is complete Forms![frmSystemConfiguration].lblStatus.caption = "Linking tables now complete." DoEvents DoCmd.Hourglass False End Sub Function Relink(strTableName As String, strSourceDatabase As String, strSourceName As String, strLocalName As String) As Boolean On Error GoTo DoLink_Error: Dim dbs As dao.Database Relink = True 'Open the backend database Set dbs = DBEngine.OpenDatabase(strSourceDatabase, False, False, ";pwd=abc") 'Delete the table, if it exists CurrentDb.TableDefs.Delete strTableName Debug.Print "DELETE: " & strTableName CurrentDb.TableDefs.Refresh 'Link the table DoCmd.SetWarnings False DoCmd.TransferDatabase acLink, "Microsoft Access", strSourceDatabase, acTable, strSourceName, strLocalName, False DoCmd.SetWarnings True Set dbs = Nothing Exit Function DoLink_Error: Select Case Err.Number Case 3011 MsgBox "Try Linking Again." & vbCrLf & Err.Description, vbCritical, APP_NAME Relink = False Exit Function Case Else Resume Next End Select End Function
Here are the Front End and the Backend databases for you to practice with (Make sure your path is correct):
Here is the front end:
How To Have Access vba link tables programmatically.accdb
Here is the backend database:
Let me know if you have any questions.
How To Parse A Flat File In Excel VBA
In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]
How can I interact with other Office applications (Excel) using VBA in Access?
Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]
How To Generate A XML File With Access VBA
XML is used to structure your data. You can send a CSV file to another person, but using XML, you can structure your data in a more elegant format, and get very specific about your fields, and what data is contained within your tags (your field names). This is different than a basic CSV file […]
Mouse Click Counter On Access Form
This post will demonstrate how you can count the number of clicks on your button in a certain time frame. It will function like a game . Here is the database form all in one: Option Compare Database Public m_dteStartTime As Date Public m_dteStopTime As Date Private Sub btnClicker_Click() Dim intValue As Integer Dim rst […]