Lost Links to External Tables.
We have already learned several methods for working with external data sources, including linking them to an MS Access database or directly opening them in Queries by setting the Source Database and SourceConnectStr Properties. In either case, the Source Data must be present in its original location at all times.
To alleviate this problem, run a check on the linked tables as soon as the Database is open for normal operations. If any linked tables lose connection to the database, warn the User and shut down the Application if it has serious implications.
How do we determine whether a linked external table has lost its connection to the Database? A simple solution is to include a procedure that regularly checks whether the external table link exists.
Immediately after the database is opened, attempt to open the linked table. If an error occurs, you can conclude that the external table link is missing.
A database may contain several tables, including both local and linked tables. How can we identify only the linked tables and attempt to open them to verify their status?
Again, this is not a serious issue, and you already have the answer if you have already gone through the earlier Articles explaining several methods of accessing external data and the usage of Connection Properties of Linked Tables and Queries.
The Connection Property Value.
We need a small VBA routine to iterate through the Table definitions and check the SourceConnectStr Property value; if the SourceConnectStr Property is not empty, then it is a linked table; otherwise, it is a local table. When we encounter a linked table, we will attempt to open it to read data. If this process triggers an Error, prepare a list of such cases and display it at the end to inform the User so that she can initiate appropriate remedial action to rectify the error.
A sample VBA routine is given below. Copy and paste the program into a Global Module and save it.
Public Function LostLinks()
'----------------------------------------------------
'Author : a.p.r. pillai
'URL : www.msaccesstips.com
'Date : 21/09/2008
'----------------------------------------------------
Dim msg As String, tbldef As TableDef
Dim strConnect As String, cdb As Database
Dim rst As Recordset, strTableName As String
Dim strDatabase As String, loc As Integer
Dim loc2 As Integer
On Error Resume Next
Set cdb = CurrentDb
For Each tbldef In cdb.TableDefs
strConnect = tbldef.Connect
If Len(strConnect) > 0 Then
strTableName = tbldef.NAME
Set rst = cdb.OpenRecordset(strTableName, dbOpenDynaset)
If Err > 0 Then
If Len(msg) = 0 Then
msg = "The following Linked Tables are missing:" & vbCr & vbCr
End If
msg = msg & strTableName & vbCr
Err.Clear
End If
rst.Close
End If
Next
If Len(msg) > 0 Then
MsgBox msg, , "LostLinks()"
End If
End Function
Call the Routine from an AutoExec macro or from the Form_Load() Event Procedure of the Application's Startup or Main Screen.










No comments:
Post a Comment
Comments subject to moderation before publishing.