ACC: How to Use OpenDatabase to Open 3rd Party Database Files

Zavrnitev odgovornosti za umaknjeno vsebino KB

Ta članek govori o izdelkih, za katere Microsoft ne ponuja več podpore. Zato je ta članek na voljo »tak, kot je«, in ne bo več posodobljen.


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

The OpenDatabase method in Visual Basic for Applications enables you to open an external database file and to work with Data Access Objects (DAO) in that database. The OpenDatabase method supports Microsoft Access databases and a number of third-party databases, including any database that supports ODBC.

The OpenDatabase method supports opening third-party databases in their native file formats in much the same way that you can link (attach) a table in Microsoft Access.

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 "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access version 2.0. For more information about Access Basic, please refer to the "Building Applications" manual.

More Information

The "Connect Property" topic in Help (or the "OpenDatabase Method (Data Access)" in Microsoft Access version 2.0) states that you use the following settings to connect to the supported database formats.

NOTE: The following table is an excerpt from Help. Please see the Help topic above in your version of Microsoft Access for a full list.

Database Type Specifier Example
dBASE IV dBASE IV; drive:\path
dBASE 5 dBASE 5.0; drive:\path
Paradox 5.x Paradox 5.x; drive:\path
FoxPro 2.5 FoxPro 2.5; drive:\path
FoxPro 2.6 FoxPro 2.6; drive:\path
Text Text; drive:\path
The Example column lists the drive and the path, but not the file name. A file name is not required for these database formats because the folder (directory) in which the files are stored is considered to be the database. If you do list a file name, you may receive unexpected results.

For example, to open a Microsoft FoxPro file named Employee.dbf located in the C:\Foxnwind folder, use the following syntax

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code.
   Set db = OpenDatabase("C:\FOXNWIND", True, False, _
"FoxPro 2.5;")
instead of:
   Set db = OpenDatabase("C:\FOXNWIND\EMPLOYEE.DBF", True, False, _
"FoxPro 2.5;")
The first example (without the file name) is the correct one to use. It sets the C:\Foxnwind folder as a database and sets the db variable to a valid Database object. The DBF files contained in the directory are considered in the database's TableDef objects.

Be aware that the second example (with the file name) is successful if you use it. The OpenDatabase method opens a database and returns a valid value without returning any errors or warnings. You can even use the database; however, the database has no TableDefs collection.

To use the OpenDatabase method to open a FoxPro 2.5 database, follow these steps:

  1. Create a C:\Foxnwind folder.
  2. Start Microsoft Access and open the sample database Northwind.mdb (or Nwind.mdb in version 2.0).
  3. Export the Employees table as a FoxPro 2.5 file to the C:\Foxnwind folder and name it Employee.dbf. (The Employees table contains memo fields and indexes and creates other supporting files in the same directory.)
  4. Export the Customers table as a FoxPro 2.5 file to the C:\Foxnwind folder and name it Customer.dbf.
  5. Create a module and type the following line in the Declarations section if it isn't already there:
    Option Explicit
  6. Type the following procedure:

          Sub OpenDatabaseTest ()
    On Local Error GoTo OpenDatabaseTest_Err
    Dim i As Integer
    Dim db As Database
    ' Open the database with the directory specified.
    Set db = OpenDatabase("C:\FOXNWIND", True, False, "FoxPro 2.5;")
    ' Loop and print the TableDefs collection.
    For i = 0 To db.tabledefs.count - 1
    Debug.Print db.tabledefs(i).name
    Next i
    Exit Sub
    MsgBox Error$
    Resume OpenDatabaseTest_End
    End Sub
  7. To test the procedure, type the following line in the Debug window (or Immediate Window in version 2.0), and then press ENTER:
    Note that "EMPLOYEE" and "CUSTOMER" appear in the Debug window.
The third-party products discussed here are manufactured by vendors independent of Microsoft; we make no warranty, implied or otherwise, regarding these products' performance or reliability.

ID članka: 132018 – Zadnji pregled: 19. jan. 2007 – Revizija: 1

Povratne informacije