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.
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
ODBC ODBC; None
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, _
Set db = OpenDatabase("C:\FOXNWIND\EMPLOYEE.DBF", True, False, _
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:
- Create a C:\Foxnwind folder.
- Start Microsoft Access and open the sample database Northwind.mdb (or Nwind.mdb in version 2.0).
- 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.)
- Export the Customers table as a FoxPro 2.5 file to the C:\Foxnwind folder and name it Customer.dbf.
- Create a module and type the following line in the Declarations section if it isn't already there:Option Explicit
- 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
- 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.
Article ID: 132018 - Last Review: Jan 19, 2007 - Revision: 1