This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Microsoft Access ODBC for Windows

This tutorial shows how to set up a data source and connect to a Microsoft® Access™ database using the Database Explorer app or the command line. This tutorial uses the Microsoft Access Driver (*.mdb, *accdb) to connect to a sample Microsoft Access 2016 database.

Step 1. Set up the sample Access database.

You can access the sample database file, tutorial.accdb, in the folder returned by entering this code at the command line.

fullfile(matlabroot,'toolbox','database','dbdemos')
Copy this database file into a folder where you have permission to write. Ensure that the database file is writable by verifying its properties:

  1. Right-click the database file and select Properties.

  2. On the General tab, if the Read-only option is selected, clear it.

Note

To write data to the sample database, ensure that you run MATLAB® as an administrator.

Note

Depending on the Access version you are running, you might need to convert the database to that version. For example, beginning in Access 2007, the software includes the option to save as *.accdb. For details, consult your database administrator.

Step 2. Verify the driver installation.

The ODBC driver is typically preinstalled on your computer. For details about the driver installation or troubleshooting the installation, contact your database administrator or refer to your database documentation on ODBC drivers. For information about the Microsoft ODBC Data Source Administrator, see Driver Installation.

Note

Database Toolbox™ no longer supports connecting to a database using a 32-bit driver. Use the 64-bit version of Access. Or, to connect to the 32-bit version of Access, see https://www.mathworks.com/matlabcentral/answers/235949-how-to-connect-to-32-bit-microsoft-access-database-from-64-bit-matlab. For details about working with the 64-bit version of Windows®, see https://www.mathworks.com/products/matlab/preparing-for-64-bit-windows.html.

Step 3. Set up the data source using the Database Explorer app.

Set up the sample Access database as the data source by using the Database Explorer app. You can locate the target database on a PC running the Windows operating system or on another system to which the PC is networked. These instructions use the Microsoft ODBC Data Source Administrator Version 10.0.16299.15 for the US English version of Microsoft Access 2016 for Windows systems.

The Database Explorer app accesses the Microsoft ODBC Data Source Administrator automatically when you configure an ODBC data source. Alternatively, you can access the Microsoft ODBC Data Source Administrator using the configureODBCDataSource function.

  1. Close any open Access databases.

  2. Open the Database Explorer app by clicking the Apps tab on the MATLAB Toolstrip. Then, on the right of the Apps section, click the Show more arrow to open the apps gallery. Under Database Connectivity and Reporting, click Database Explorer. Alternatively, enter databaseExplorer at the command line.

  3. In the Data Source section, select Configure Data Source > Configure ODBC data source.

    In the ODBC Data Source Administrator dialog box, you define the ODBC data source.

    Tip

    When setting up an ODBC data source, you can specify a user data source name (DSN) or a system DSN. A user DSN is specific to the person logged into a machine. Only this person sees the data sources that are defined on the user DSN tab. A system DSN is not specific to the person logged into a machine. Any person who logs into the machine can see the data sources that are defined on the system DSN tab. Your ability to set up a user DSN or system DSN depends on the database and ODBC driver you are using. For details, contact the database administrator or refer to the ODBC driver documentation.

  4. On the User DSN tab, click Add. The Create New Data Source dialog box opens and displays a list of installed ODBC drivers.

  5. Select Microsoft Access Driver (*.mdb, *.accdb) and click Finish.

  6. In the ODBC Microsoft Access Setup dialog box for your driver, enter dbdemo as the data source name. Enter tutorial database as the description.

  7. Click Select to open the Select Database dialog box, where you specify the database you want to use. For the dbdemo data source, select tutorial.accdb. If the database is on a system to which your PC is connected:

    1. Click Network.

    2. In the Map Network Drive dialog box, specify the folder containing the database you want to use. Ensure that you map to the folder and not the database file.

    3. Click Finish.

  8. Click OK to close the Select Database dialog box. In the ODBC Microsoft Access Setup dialog box, click OK. The ODBC Data Source Administrator dialog box displays the dbdemo data source and any additional data sources that you added on the User DSN tab. Click OK to close the dialog box.

After you complete the data source setup, connect to the Access database using the Database Explorer app or the command line with the native ODBC connection.

Step 4. Connect using the Database Explorer app or the command line.

Connect to Access Using Database Explorer App

  1. On the Database Explorer tab, in the Data Source section, click New Query.

  2. In the Connect to a Data Source dialog box, select the data source dbdemo from the Data Source list.

  3. Leave the Username and Password boxes blank, and click Connect.

    The Database Explorer app connects to the database and displays a list of its objects, such as tables, in the Data Browser pane. The data source tab named dbdemo appears to the right of the pane. The data source tab contains empty SQL Query and Data Preview panes.

  4. Select tables in the Data Browser pane to query the database.

  5. Close the dbdemo data source tab to close the SQL query and the database connection.

    Tip

    If multiple database connections to different databases are open, then close the Database Explorer app to close all database connections.

Connect to Access Using ODBC Driver and Command Line

  1. Connect to the database with the ODBC data source name. For example, this code assumes that you are connecting to a data source named dbdemo with a blank user name and password.

    conn = database('dbdemo','','');
    
  2. Close the database connection.

    close(conn)

See Also

Apps

Functions

Related Topics