Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Create a Customized "Attach ODBC Table" Form


View products that this article applies to.

This article was previously published under Q210295
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies only to a Microsoft Access database (.mdb).

↑ Back to the top


Summary

This article describes how to create a form that uses the TransferDatabase command to attach tables from an Open Database Connectivity (ODBC) data source. Creating this form provides more control over the tables than the Link dialog box and is faster than modifying the AttachableObjects setting in the registry.

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To create a sample form to make linked ODBC tables, you must first create a table or query that contains the names of the tables on the server. Then you can create a form based on that query to create the links to the tables you select from a list.

Create the Query Containing the Table List

To create a query that lists the tables on a server, follow these steps:
  1. Start Microsoft Access and open any database.
  2. In the Database window, click Queries, and then click New.
  3. Click Design View, and then click OK.
  4. Close the Show Tables dialog box without adding any tables.
  5. On the Query menu, point to SQL Specific, and then click Pass-Through.
  6. In the SQL Pass-Through Query window, type the following SQL statement to return a list of all user tables:
    SELECT name FROM sysobjects WHERE type='U'
  7. On the View menu, click Properties.
  8. In the ODBC Connect Str property, type the ODBC connect string for the SQL pass-through query. This example uses the Pubs database that is provided with Microsoft SQL Server 7.0. You can use the following line as a template and substitute the correct Data Source Name (DSN), User ID (UID), Password (PWD), and Database:
    ODBC;DSN=MyServer;UID=MyUserId;PWD=MyPassword;LANGUAGE=us_english;DATABASE=pubs
  9. Run the query to make sure it returns the expected table names. Save this SQL pass-through query as Query1.

Create the Form to Use to Attach Tables

To create a form that you can use to attach tables to your current database, follow these steps:
  1. Create a new unbound form and add an unbound list box and command button to the form, with the following properties:
    Form: frmAttachTable

    List Box
    Name: Attach
    Row Source: Query1

    Command Button
    Name: cmdAttachTable
    Caption: Attach Table
    Save the form as frmAttachTable.
  2. Set the On Click property of the cmdAttachTable command button to the following event procedure:
    Private Sub cmdAttachTable_Click()
    
    If MsgBox("Attach table " & Forms!frmAttachTable!Attach & "?") Then
    
        'Substitute the correct ODBC connection parameters
        'for the Pubs database.
    
        DoCmd.TransferDatabase acLink, "ODBC", _
        "ODBC;DSN=MyServer;UID=MyUserId;PWD=MyPassword;LANGUAGE=us_english; _
        & "DATABASE = pubs", acTable, Forms!frmAttachTable!Attach, _
        "Local" & _
        Forms!frmAttachTable!Attach
    
    End If
    End Sub
    					
  3. On the View menu, click Form View. Note that the list box lists the table names. You can select a table name in the list box and then click the Attach Table button to create the link to the table in your current database.

↑ Back to the top


References

For more information about TransferDatabase Method, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type TransferDatabase Method in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB210295, kbusage, kbhowto

↑ Back to the top

Article Info
Article ID : 210295
Revision : 3
Created on : 6/23/2005
Published on : 6/23/2005
Exists online : False
Views : 390