Using the Connectivity Kit to Connect to Microsoft Access

ID: Q116072

2.50 2.50a 2.50b 2.60 WINDOWS kbinterop

The information in this article applies to:

SUMMARY

This article describes how to use the FoxPro Connectivity Kit and ODBC to access data that resides in a Microsoft Access database.

MORE INFORMATION

This article assumes that you have the MS Access ODBC driver that is available as a part of the Microsoft ODBC Desktop Database Driver Kit (DDDK). This driver is also included with some Microsoft products, such as Microsoft Word for Windows and Microsoft Excel for Windows.

To connect to a Microsoft Access data source, you must install the MS Access ODBC Driver and set up an MS Access data source through the ODBC MS Access Setup dialog box in the Windows Control Panel. Since this is a level 1 ODBC driver, the data source is an MS Access database (.MDB) name, which you select through the Select Database dialog box.

The MS Access ODBC Setup Dialog also contains an option to specify the System Database, if required.

The following is a code sample that "connects" to an MS Access data source:

   *-- Connect to an MS Access Data Source
   PRIVATE lcSource, lcUser, lcPwd, hDBHdle, lnDBRes

   *-- Init memvars
   STORE 0 TO hDBHdle, lnDBRes
   lcSource = "Access"      && MS Access Data Source Name from ODBC Dialog
   lcUser = ""              && Not needed for single-tier drivers
   lcPwd = ""               && Not needed for MS Access

   *-- Load Library if not already
   IF !"FPSQL" $ SET('library')
        SET LIBRARY TO FPSQL.FLL
   ENDIF
   WAIT WINDOW "Attempting connection ..." NOWAIT
   hDBHdle = dbConnect(lcSource, lcUser, lcPwd, "")
   IF hDBHdle > 0
      WAIT WINDOW ;
    "Connected to Access -  Handle:"+STR(hDBHdle,1,0) TIMEOUT 2
   ELSE
      WAIT WINDOW ;
    "Unable to connect to Access .."
   ENDIF

To change the database being used by the MS Access connection, USE the new database in a DBExec() statement:

   lnDbRes = DBExec(hDBHdle, "USE c:\access\nwind.mdb")
   *(change drive, directory and database name to suit)

REFERENCES

Microsoft ODBC Desktop Database Driver Kit MS Access Driver online Help file

Additional reference words: FoxWin 2.50 2.50a 2.50b 2.60 ck KBCategory: kbinterop KBSubcategory: FxtoolCk

Keywords          : kbcode FxtoolCk 
Version           : 2.50 2.50a 2.50b 2.60
Platform          : WINDOWS

Last Reviewed: May 22, 1998