Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] - نسخه متنی

Alison Balter

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



Creating a Link to External Data


If you need to keep the data in its original format but want to treat the data just like any other Access table, linking is the best solution. All the information required to establish and maintain the connection to the remote data source is stored within the linked table definition. You can create links through the user interface and by using code. This section covers both alternatives.Chapter 20 discusses these benefits in more detail.

Using the User Interface


It is very common to create a link using the user interface. If you know what links you want to establish at design time, this is probably the easiest way to establish links to external data. You can establish links using the Database Splitter or manually.

Creating Links Using the Database Splitter

The Database Splitter was designed to split databases that already have been built with the tables and other database objects all in one physical MDB database file. It automates the process of moving the data tables to another database. The Database Splitter is covered in detail in Chapter 20.

NOTE

You can create links to tables only from an Access database, not from an Access project.

Creating Links to Access Tables Manually

To create a link to an Access table, follow these steps:


  • Right-click anywhere within the Database window.

  • Choose Link Tables. The Link dialog box appears, as shown in Figure 19.3.

    Figure 19.3. The Link dialog box.

  • Select the name of the database containing the table to which you want to link.

  • Click the Link button. The Link Tables dialog box appears, as shown in Figure 19.4.

    Figure 19.4. The Link Tables dialog box.

  • Select the tables to which you want to establish a link.

  • Click OK. The link process finishes. Notice the arrows in Figure 19.5, which indicate that the tables are linked tables instead of tables stored in the current database.

    Figure 19.5. Linked tables in the Database window.


  • Creating Links to Other Types of Tables Manually

    The process of creating links to other types of database files is a little different. It works like this:


  • Right-click anywhere within the Database window.

  • Choose Link Tables. The Link dialog box appears.

  • In the Files of Type drop-down list, select the type of table to which you are linking.

  • Select the external file containing the data to which you will be linking.

  • Click the Link button. The next dialog box varies, depending on the type of table to which you want to link. With a dBASE file, for example, the Select Index Files dialog box appears, as shown in Figure 19.6. It is important that you select any index files associated with the data file. These indexes are updated automatically by Access as you add, change, and delete table data from within Access.

    Figure 19.6. The Select Index Files dialog box.

  • You receive a message indicating that the index was added successfully and that you can add other indexes if you choose. Click OK.

  • Add any additional indexes and click Close.

  • The Select Unique Record Identifier dialog box appears, as shown in Figure 19.7 (see the following Note if the dialog doesn't appear). This dialog box enables you to select a unique identifier for each record in the table. Select a unique field and click OK.

    Figure 19.7. The Select Unique Record Identifier dialog box.


  • NOTE

    You must install the Jet 4.0 Service Pack 5 or have the BDE (Borland Database Engine) for the Select Unique Record Identifier dialog box to appear. If you do not have one of these tools, the data in the database cannot be edited. For more information, see the Microsoft Knowledge Base article Q283294.

    Notice the icon indicating the type of file you linked to, as shown in Figure 19.8. You can now view the table data, build queries and reports from the data, and more!

    Figure 19.8. An icon indicating that the file database is linked to a dBASE database file.


    NOTE

    Earlier versions of Access supported links to FoxPro files using the FoxPro ISAM driver. With Jet 4.0, you must link to FoxPro tables using the Visual FoxPro ODBC Driver.

    Using Code


    Creating a link to an external table using code is a six-step process. Here are the steps involved in establishing the link:


  • Create a reference to the Microsoft ADO Extension 2.6 for DDL and Security (ADOX) library.

  • Create a Catalog object.

  • Set the Connection property of the Catalog object to the database that will contain the linked table.

  • Create a new Table object.

  • Set properties of the Table object.

  • Append the Table object to the Catalog.


  • Listing 19.4 shows the code for linking to an external table, which, in this case, exists in another Microsoft Access database.

    Listing 19.4 Linking to an External Table

    Sub LinkToAccessTableProps()
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
    'Instantiate a Catalog Object
    Set cat = New ADOX.Catalog
    'Set the connection of the Catalog object
    'to the connection associated with the current
    'project
    cat.ActiveConnection = CurrentProject.Connection
    'Instantiate a table object
    Set tbl = New ADOX.Table
    'Establish the name of the new table object
    tbl.Name = "tblLinkedTable"
    'Point the catalog of the new table at the
    'catalog object established above
    Set tbl.ParentCatalog = cat
    'Set necessary properties of the new table object
    tbl.Properties("Jet OLEDB:Create Link") = True
    tbl.Properties("Jet OLEDB:Link Datasource") = _
    CurrentProject.Path & "\Chap19Data.mdb"
    tbl.Properties("Jet OLEDB:Link Provider String") = ";pwd=password"
    tbl.Properties("Jet OLEDB:Remote Table Name") = "tblClients"
    'Append the new table object to the tables collection
    'of the Catalog object
    cat.Tables.Append tbl
    End Sub

    In Listing 19.4, a Catalog object is created. The ActiveConnection property of the Catalog object is pointed at the connection associated with the current database. Next, a Table object is created. The Name property of the Table object is set equal to tblLinkedTable. The ParentCatalog property of the Table object is set to point at the Catalog object. Four properties in the properties collection of the Table object are set to the appropriate values, and the Table object is appended to the Catalog object. This process is discussed in further detail in the following sections.

    Providing Connection Information

    When you link to an external table, you must provide information about the type, name, and location of the external database. You accomplish this by setting the following properties in the Properties collection of the Table object:

    • Jet OLEDB:Link Provider String

    • Jet OLEDB:Remote Table Name

    • Jet OLEDB:Link Datasource


    The following three lines of code illustrate the process of setting the provider string, name, and location of the source table:

    tbl.Properties("Jet OLEDB:Link Provider String") = ";pwd=password"
    tbl.Properties("Jet OLEDB:Remote Table Name") = "tblClients"
    tbl.Properties("Jet OLEDB:Link Datasource") = CurrentProject.Path & "\Chap19Data.mdb"

    The Jet OLEDB:Link Provider is the ISAM format that will be used for the link. Each source database type is a different folder in the Windows registry. The folders are located in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\ISAM Formats section of the registry. Valid source database types are as follows:

    dBASE

    dBASE III, dBASE IV, and dBASE 5.0

    Excel

    Excel 3.0, Excel 4.0, Excel 5.0, and Excel 8.0

    HTML

    HTML Export and HTML Import

    Jet

    Jet 2.

    x , Jet 3.

    x

    Lotus

    Lotus WK1, Lotus WK3, Lotus WK4, Lotus WJ2, and Lotus WJ3

    Exchange

    Exchange 4.0

    Outlook

    Outlook 9.0

    Paradox

    Paradox 3.

    x , Paradox 4.

    x , Paradox 5.

    x , and Paradox 7.

    x

    SharePoint Team Services

    2.0

    Text

    N/A

    Windows SharePoint Services

    N/A

    The Jet OLEDB:Link Datasource must include a fully qualified path to the file. You can specify the path with a drive letter and directory path or by using

    universal naming conventions (UNCs) . For a local database, you must specify the path like this:

    tbl.Properties("Jet OLEDB:Link Datasource") = "c:\Databases\Chap19Data"

    For a file server, you can specify the UNC path or the drive letter path. The UNC path looks like this:

    tbl.Properties("Jet OLEDB:Link Datasource") = _
    "\\filesERVERNAME\Databases\Chap19Data"

    In this case, the database called Chap19Data is stored on the databases share of a particular file server.

    Creating the Link

    Listing 19.5 shows how you put all the connection information together to establish a link to an external table.

    Listing 19.5 Establishing a Link to an External Table

    Sub LinkToDBase(strDirName As String, strTableName As String, _
    strAccessTable)
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
    'Instantiate a Catalog object
    Set cat = New ADOX.Catalog
    cat.ActiveConnection = CurrentProject.Connection
    'Instantiate a Table object
    Set tbl = New ADOX.Table
    tbl.Name = strAccessTable
    Set tbl.ParentCatalog = cat
    'Set necessary properties of the new Table object
    tbl.Properties("Jet OLEDB:Create Link") = True
    tbl.Properties("Jet OLEDB:Link Datasource") = strDirName
    tbl.Properties("Jet OLEDB:Link Provider String") = "dBASE III;HDR=NO;IMEX=2;"
    tbl.Properties("Jet OLEDB:Remote Table Name") = strTableName
    'Append the new table object to the tables collection
    'of the Catalog object
    cat.Tables.Append tbl
    End Sub

    Here is an example of how you call this subroutine:

    Call LinkToDBase("c:\customer\data","customer","tblCustomers")

    The LinkToDBase subroutine receives three parameters:

    • The name of the directory in which the dBASE file is stored

    • The name of the file (the name of the table, without the DBF extension) to which you want to connect

    • The name of the Access table that you are creating


    The subroutine creates two object variables: a Catalog object variable and a Table object variable. It points the ActiveConnection property of the Catalog object variable at the connection associated with the current database. Next, it establishes properties of the Table object. The Link Datasource is the name of the directory within which the dBASE file is stored. The Link Provider String specifies that the type of table you are linking to is a dBASE III file. The Remote Table Name is the name of the dBASE file that you are linking to. After setting these properties, you are ready to append the table definition to the database.

    You have seen how you can link to a dBASE table. Listing 19.6 puts together everything you have learned thus far in this chapter by creating a link to an Access table stored in another database.

    Listing 19.6 Creating a Link to an Access Table Stored in Another Database

    Sub LinkToAccess(strDBName As String, strTableName As String, _
    strAccessTable)
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
    'Instantiate a Catalog object
    Set cat = New ADOX.Catalog
    'Set the ActiveConnection property of the Catalog object
    'to the connection associated with the current project
    cat.ActiveConnection = CurrentProject.Connection
    'Instantiate a Table object
    Set tbl = New ADOX.Table
    'Set the Name property of the Table object to the name
    'you wish to give to the linked table
    tbl.Name = strAccessTable
    'Set the ParentCatalog property of the Table object
    'to the Catalog object
    Set tbl.ParentCatalog = cat
    'Set all necessary properties of the Table object
    tbl.Properties("Jet OLEDB:Create Link") = True
    tbl.Properties("Jet OLEDB:Link Datasource") = strDBName
    tbl.Properties("Jet OLEDB:Link Provider String") = ";pwd=password"
    tbl.Properties("Jet OLEDB:Remote Table Name") = strTableName
    'Append the Table object to the Tables collection
    'associated with the Catalog object
    cat.Tables.Append tbl
    End Sub

    Notice that the Jet OLEDB Link Provider string no longer specifies the type of database to which you are connecting. Everything else in this routine is the same as the routine that connected to dBASE. Also, notice the parameters passed to this routine:

    Call LinkToAccess("C:\databases\northwind.mdb","Customers","tblCustomers")

    The database passed to the routine is an actual Access database (as opposed to a directory), and the table name is the name of the Access table in the other database (instead of the DBF filename).

    NOTE

    Whether you link to an external database using the user interface or code, you should always use the UNC path, rather than a drive letter. This ensures that all users with access to the network share are able to see the data, regardless of their drive letter mappings.


    / 544