Microsoft KB Archive/166965

{| = ACC: How to Relink Back-End Tables with Common Dialog Control =
 * width="100%"|

Last reviewed: August 29, 1997

Article ID: Q166965 The information in this article applies to:
 * Microsoft Access versions 7.0, 97

SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article contains an example that shows you how to refresh linked tables in your database using the Common Dialog control that is available with Microsoft Office 97 Developer Edition Tools or the Microsoft Access Developer's Toolkit version 7.0.

For an example of a different method to relink tables that uses Windows application programming interface (API) functions, refer to the Developer Solutions sample application (Solutions.mdb) included with Microsoft Access 7.0 and 97. Open the Solutions.mdb database, select &quot;Use multiple databases&quot; in the &quot;Select a Category of Examples&quot; box, and then &quot;Link tables at startup&quot; in the Select An Example box.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the &quot;Building Applications with Microsoft Access&quot; manual.

MORE INFORMATION
An application that uses split database design has its tables in one database in a shared network location (the back-end database), and all its queries, forms, reports, macros and modules in another database on each client computer (the front-end database). The front-end database links all of its tables to the back-end database. If the back-end database is moved, errors occur in your application. You can build functionality into your application to detect that the back-end database file is missing from its expected location, and then prompt the user for the new location.

The following example demonstrates a method for relinking table data in your application. This method creates a form that a user can open to relink the back-end tables, and optionally uses a form to automatically verify the back-end link behind the scenes.

This article assumes that you have the Microsoft Common Dialog control, which is available with the Microsoft Office 97 Developer Edition and the Microsoft Access Developer's Toolkit version 7.0. If you do not have this control, skip the Sub procedure cmdBrowse_Click in the example.

NOTE: If you use the Common Dialog control and you plan to distribute your database application, you must include the Common Dialog Control file, Comdlg32.ocx, and its supporting DLLs with your setup files. For more information about which supporting files to include with ActiveX controls in Microsoft Office 97 Developer Edition applications, search the Help Index for &quot;ActiveX controls, files required for.&quot; For more information about which supporting files to include with OLE Custom Controls in Microsoft Access Developer's Toolkit version 7.0 applications, click the Help button on the &quot;Add the files that you want your custom Setup program to copy&quot; screen of the Setup Wizard.

Method to Relink Back-End Database Tables
 Create a new blank database called FrontEnd.mdb. On the File menu, point to Get External Data, and then click Link Tables. Create a link to each of the tables in the sample database Northwind.mdb.  Create a new form not based on any table or query in Design view: Form: frmNewDataFile - Text box: Name: txtFileName Width: 3&quot; Common Dialog control: Name: xDialog Command button: Name: cmdBrowse Caption: Browse... Command button: Name: cmdLinkNew Caption: Refresh Links Command button: Name: cmdCancel Caption: Cancel Cancel: Yes  On the View menu, click Code.  Type the following procedures: '****************************************************************** ' The TablesMatch function assumes that strFileName exists and is a ' Microsoft Access database. ' Checks table names in strFileName to ensure they have a matching ' link in the current database. '****************************************************************** Private Function TablesMatch(strFileName As String) As Integer On Error GoTo Err_TablesMatch Dim intFlag As Integer, dbLocal As Database, dbBackEnd As Database Dim tdLocal As TableDef, tdBackEnd As TableDef intFlag = False  ' Initial value, process by exception.

Set dbLocal = CurrentDb Set dbBackEnd = DBEngine(0).OpenDatabase(strFileName) For Each tdLocal In dbLocal.TableDefs  ' Loop through local tables. If Len(tdLocal.Connect) > 0 Then ' This is an attached table. For Each tdBackEnd In dbBackEnd.TableDefs ' Loop thru back-end If tdLocal.SourceTableName = tdBackEnd.Name Then intFlag = True ' We found a match in back-end. Exit For  ' Jump out for next search. End If            Next If intFlag = False Then ' No match for this attached table. TablesMatch = False Exit Function Else ' There was a match--continue searching next attachment. intFlag = False ' Reset flag for next table test. End If        End If      Next ' If the function reached here, all tables matched. TablesMatch = True Exit_TablesMatch: Exit Function Err_TablesMatch: MsgBox &quot;The file isn't an MS Access Database or may be corrupted.&quot; TablesMatch = False Resume Exit_TablesMatch End Function

Private Sub cmdLinkNew_Click On Error GoTo Err_cmdLinkNew_Click Dim strTest As String, dbLocal As Database Dim tdLocal As TableDef On Error Resume Next  ' Turn off error check for test. strTest = Dir(Me![txtFileName]) On Error GoTo Err_cmdLinkNew_Click If Len(strTest) = 0 Then  ' File not found. MsgBox &quot;File not found. Please try again.&quot;, vbExclamation, _ &quot;Link to new data file&quot; ElseIf TablesMatch(Me![txtFileName]) Then  ' A valid database. Set dbLocal = CurrentDb DoCmd.Hourglass True For Each tdLocal In dbLocal.TableDefs  ' Loop through all tables. If Len(tdLocal.Connect) > 0 Then  ' This is an linked table. DoCmd.Echo True, &quot;Linking &quot; & tdLocal.Name tdLocal.Connect = &quot;;DATABASE=&quot; & Trim(Me![txtFileName]) tdLocal.RefreshLink  ' Commit table link to new location. End If        Next DoCmd.Echo True, &quot;Done&quot; DoCmd.Hourglass False MsgBox &quot;Linking to new back-end data file was successful.&quot; DoCmd.Close acForm, Me.Name  ' Close the form. Else  ' Tables didn't match the tablenames in this database. MsgBox &quot;The tables in data file &quot; & Me![txtFileName] & _ &quot; didn't match the current database&quot; End If     Exit_cmdLinkNew_Click: DoCmd.Echo True  ' Just in case of error jump. DoCmd.Hourglass False Exit Sub Err_cmdLinkNew_Click: MsgBox Err.Description Resume Exit_cmdLinkNew_Click End Sub

Private Sub cmdCancel_Click On Error GoTo Err_cmdCancel_Click MsgBox &quot;Link to new back-end cancelled&quot;, vbExclamation, _ &quot;Cancel Refresh Link&quot;  ' Give a warning of cancellation. DoCmd.Close acForm, Me.Name  ' Close the form. Exit_cmdCancel_Click: Exit Sub Err_cmdCancel_Click: MsgBox Err.Description Resume Exit_cmdCancel_Click End Sub

Private Sub cmdBrowse_Click ' Prompts user for back-end database file name. On Error GoTo Err_cmdBrowse_Click Dim strFileName As String Dim oDialog As Object Set oDialog = Me!xDialog.Object With oDialog  ' Ask for new file location. .DialogTitle = &quot;Please Select New Data File&quot; .Filter = &quot;Access Database(*.mdb;*.mda;*.mde;*.mdw)|&quot; & _ &quot;*.mdb; *.mda; *.mde; *.mdw|All(*.*)|*.*&quot; .FilterIndex = 1 .ShowOpen ' If user responded, put selection into textbox on form. If Len(.FileName) > 0 Then Me![txtFileName] = .FileName End With Exit_cmdBrowse_Click: Exit Sub Err_cmdBrowse_Click: MsgBox Err.Description Resume Exit_cmdBrowse_Click End Sub  On the Debug menu, click &quot;Compile and Save All Modules&quot; (in Microsoft Access 7.0, on the Run menu click &quot;Compile All Modules&quot;; then on the File menu, click &quot;Save All Modules&quot;). Save the frmNewDataFile form and close it. Move the Northwind.mdb sample database to another folder on your hard drive so the linked tables in FrontEnd.mdb will need to be refreshed.</li> Open the frmNewDataFile form, and click the Browse button.</li> In the &quot;Please Select New Data File&quot; dialog box, locate Northwind.mdb in its new folder, and then click Open. Note that the path and file name of the database appears in the text box on your form.</li>  Click the Refresh Links button on the form. Note that each table name displays on the status line as the link is refreshed, and that you receive the following message when the procedure is done: Linking to new back-end data file was successful. </li></ol>

Creating a Startup Form to Check Linked Tables
If you want to verify the linked tables automatically each time you open the FrontEnd.mdb database, you can follow these steps to create a hidden form for that purpose:

<ol> Create a new form not based on any table or query in Design view.</li> On the View menu, click Code.</li>  Type the following procedure to execute when the form opens: Private Sub Form_Open(Cancel As Integer) ' Tests a linked table for valid back-end. On Error GoTo Err_Form_Open Dim strTest As String, db As Database Dim td As TableDef Set db = CurrentDb For Each td In db.TableDefs If Len(td.Connect) > 0 Then ' Is a linked table. On Error Resume Next ' Turn off error trap. strTest = Dir(Mid(td.Connect, 11)) ' Check file name. On Error GoTo err_Form_Open ' Turn on error trap. If Len(strTest) = 0 Then ' No matching file. If MsgBox(&quot;Couldn't find the back-end file &quot; & _ Mid(td.Connect, 11) & &quot;. Please choose new data file.&quot;, _ vbExclamation + vbOKCancel + vbDefaultButton1, _ &quot;Can't find backend data file.&quot;) = vbOK Then DoCmd.OpenForm &quot;frmNewDataFile&quot; ' Open prompt form. Else MsgBox &quot;The linked tables can't find their source. &quot; & _ &quot;Please log onto network and restart the application.&quot; End If End If DoCmd.Close acForm, Me.Name Exit For ' Stop at one linked table. End If Next ' Loop to next tabledef. Exit_Form_Open: Exit Sub Err_Form_Open: MsgBox &quot;Oops! &quot; & Error.Description Resume Exit_Form_Open End Sub </li> On the Debug menu, click &quot;Compile and Save All Modules&quot; (in Microsoft Access 7.0, on the Run menu click &quot;Compile All Modules&quot;; then on the File menu, click &quot;Save All Modules&quot;).</li> Save the form as frmCheckLink, and then close it.</li> Set frmCheckLink as your Startup form by clicking Startup on the Tools menu. In the Startup dialog box, select frmCheckLink in the Display Form box, and then click OK.</li> Make frmCheckLink a hidden form by using the right mouse button (right-click) to click frmCheckLink in the Database window, and then click Properties on the shortcut menu that appears. Click the Hidden check box in the frmCheckLink Properties dialog box, and then click OK.</li> Move the Northwind.mdb sample database to another folder on your hard drive so the linked tables in FrontEnd.mdb will need to be refreshed.</li>  Close and then reopen FrontEnd.mdb. Note that you receive the following message: Couldn't find the back-end file <Database Name>. Please choose new data file. If you click OK, the frmNewDataFile form opens for you to select a new back-end database, and then refreshes your table links. If you click Cancel, you receive this message: The linked tables can't find their source. Please log onto network and restart the application. </li></ol>