Snowflake68
Registered User.
- Local time
- Today, 02:43
- Joined
- May 28, 2014
- Messages
- 464
NO HELP REQUIRED TODAY, IM JUST PROVIDING INFORMATION SO THAT OTHERS CAN USE.
I have a split database with the tables in an Access database (2013) as the backend and all the forms and queries in another for the front end.
The system will be distributed to various users on different setups for them to use independently. So in order to maintain the linked tables I have found some code on a Microsoft blog (by 'Courtney Owen') that dynamically relinks them as long as the backend is in the same directory as the front end application. This all works perfectly although it did take me a while to fix the errors as the code I found wasnt a perfect working solution for me.
Here is the function 'RefreshTableLinks' which is called from a splash screen on launch of the front end application.
I wanted to change the code so that the backend sits in a separate sub directory but I didnt know how to change the code in order to achieve this. But I have persevered without asking for help from all you helpful people on here and have managed to achieved it all on my own.
I just wanted to share my final code to give something back on here so that others could use it too.
This is the code on my Splash screen. It needs to call the function before opening any form that is bound to a linked table.
I hope this helps others that want to split their databases and hold the backend database in a sub directory of the application, whilst dynamically relinking the tables.
I have a split database with the tables in an Access database (2013) as the backend and all the forms and queries in another for the front end.
The system will be distributed to various users on different setups for them to use independently. So in order to maintain the linked tables I have found some code on a Microsoft blog (by 'Courtney Owen') that dynamically relinks them as long as the backend is in the same directory as the front end application. This all works perfectly although it did take me a while to fix the errors as the code I found wasnt a perfect working solution for me.
Here is the function 'RefreshTableLinks' which is called from a splash screen on launch of the front end application.
Code:
Public Function RefreshTableLinks() As String
On Error GoTo ErrHandle
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strCon As String
Dim strBackEnd As String
Dim strMsg As String
Dim intErrorCount As Integer
Set db = CurrentDb
'Loop through the TableDefs Collection.
For Each tdf In db.TableDefs
'Verify the table is a linked table.
If Left$(tdf.Connect, 10) = ";DATABASE=" Then
'Get the existing Connection String.
strCon = Nz(tdf.Connect, "")
'Get the name of the back-end database using String Functions.
strBackEnd = Right$(strCon, (Len(strCon) - (InStrRev(strCon, "\") - 1)))
'Verify we have a value for the back-end
If Len(strBackEnd & "") > 0 Then
'Set a reference to the TableDef Object.
Set tdf = db.TableDefs(tdf.Name)
'Build the new Connection Property Value.
tdf.Connect = ";DATABASE=" & CurrentProject.Path & strBackEnd
'Refresh the table link.
tdf.RefreshLink
Else
'There was a problem getting the name of the back-end.
'Add the information to the message to notify the user.
intErrorCount = intErrorCount + 1
strMsg = strMsg & "Error getting back-end database name." & vbNewLine
strMsg = strMsg & "Table Name: " & tdf.Name & vbNewLine
strMsg = strMsg & "Connect = " & strCon & vbNewLine
End If
End If
Next tdf
ExitHere:
On Error Resume Next
If intErrorCount > 0 Then
strMsg = "There were errors refreshing the table links: " _
& vbNewLine & strMsg & "In Procedure RefreshTableLinks"
RefreshTableLinks = strMsg
End If
Set tdf = Nothing
Set db = Nothing
Exit Function
ErrHandle:
intErrorCount = intErrorCount + 1
strMsg = strMsg & "Error " & Err.Number & " " & Err.Description
strMsg = strMsg & vbNewLine & "Table Name: " & tdf.Name & vbNewLine
strMsg = strMsg & "Connect = " & strCon & vbNewLine
Resume ExitHere
End Function
I wanted to change the code so that the backend sits in a separate sub directory but I didnt know how to change the code in order to achieve this. But I have persevered without asking for help from all you helpful people on here and have managed to achieved it all on my own.
I just wanted to share my final code to give something back on here so that others could use it too.
Code:
Public Function RefreshTableLinks() As String
On Error GoTo ErrHandle
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strCon As String
Dim strBackEnd As String
Dim strMsg As String
Dim intErrorCount As Integer
Set db = CurrentDb
'Loop through the TableDefs Collection.
For Each tdf In db.TableDefs
'Verify the table is a linked table.
If Left$(tdf.Connect, 10) = ";DATABASE=" Then
'Get the existing Connection String.
strCon = Nz(tdf.Connect, "")
'Get the name of the back-end database using String Functions.
strBackEnd = Right$(strCon, (Len(strCon) - (InStrRev(strCon, "Data\") - 1))) ' amended this line to include the backend sub directory 'Data'
'Verify we have a value for the back-end
If Len(strBackEnd & "") > 0 Then
'Set a reference to the TableDef Object.
Set tdf = db.TableDefs(tdf.Name)
'Build the new Connection Property Value.
tdf.Connect = ";DATABASE=" & CurrentProject.Path & "\" & strBackEnd ' amended this line to include "\" &
'Refresh the table link.
tdf.RefreshLink
Else
'There was a problem getting the name of the back-end.
'Add the information to the message to notify the user.
intErrorCount = intErrorCount + 1
strMsg = strMsg & "Error getting back-end database name." & vbNewLine
strMsg = strMsg & "Table Name: " & tdf.Name & vbNewLine
strMsg = strMsg & "Connect = " & strCon & vbNewLine
End If
End If
Next tdf
ExitHere:
On Error Resume Next
If intErrorCount > 0 Then
strMsg = "There were errors refreshing the table links: " _
& vbNewLine & strMsg & "In Procedure RefreshTableLinks"
RefreshTableLinks = strMsg
End If
Set tdf = Nothing
Set db = Nothing
Exit Function
ErrHandle:
intErrorCount = intErrorCount + 1
strMsg = strMsg & "Error " & Err.Number & " " & Err.Description
strMsg = strMsg & vbNewLine & "Table Name: " & tdf.Name & vbNewLine
strMsg = strMsg & "Connect = " & strCon & vbNewLine
Resume ExitHere
End Function
This is the code on my Splash screen. It needs to call the function before opening any form that is bound to a linked table.
Code:
Private Sub Form_Open(Cancel As Integer)
DoCmd.ShowToolbar "Ribbon", acToolbarNo
DoCmd.Maximize
RefreshTableLinks
Dim strMsg As String
'Run the Procedure, getting any error messages.
strMsg = RefreshTableLinks()
'strMsg will be a zero-length string if there is no error message.
If Len(strMsg & "") = 0 Then
Debug.Print "All; Tables; were; successfully; relinked."
Else
'Notify the user of the errors.
MsgBox strMsg, vbCritical
End If
End Sub
I hope this helps others that want to split their databases and hold the backend database in a sub directory of the application, whilst dynamically relinking the tables.