Warning

Please note that the documentation you are currently viewing is for an older version of our technology. While it is still functional, we recommend upgrading to our latest and more efficient option to take advantage of all the improvements we’ve made.

Connection String for Access Database Files

AIMMS has the possibility to retrieve/store data from/into any ODBC datasource. You could provide a System/User/File DSN (in case of ODBC) pointing to the database as an input to AIMMS. You could also dynamically create a connection string instead of using a DSN file.

This article shows how to generate a connection string to connect to an Access database via the ODBC layer.

Advantages of the Connection String

When you deploy a project where the end-user can specify an Access database file, you have three options:

  • generate the DSN files based on the user’s choice

  • let the user choose the specific Access database file via the file selection wizards of the ODBC driver

  • generate the connection string within AIMMS itself, based on the file that the user selected

One advantage of creating a connection string instead of using DSN files is that you don’t have to include the database password in the plain-text DSN file if your database requires a password; you can keep this password hidden in the code of your project.

These connection strings can be used in the Data Source attributes of all the database related identifiers AIMMS (e.g. tables, database procedures).

Implementation of the Procedure

To create a connection string for a given Access database file (.mdb or .accdb), the procedure CreateAccessFileConnectionString below uses the intrinsic AIMMS function SQLCreateConnectionString().

The procedure will first query which ODBC drivers are available, and follow with these actions:

  1. try to find the driver that supports the Office 2007 and later databases (i.e. the Access files with extension .accdb)

  2. if the driver cannot be found, search for the driver that supports .mdb files.

  3. if that driver cannot be found, raise an error

The procedure requires two arguments:

  • AccessDatabaseFile: Input argument denoting which database to use,

  • ConnectionString: Output argument in which the connection string is stored.

Furthermore, the procedure has the following two local identifiers:

Set sODBCDrivers {
    Index: iOdbcDrivers;
}
ElementParameter epODBCDriver {
    Range: sODBCDrivers;
}
 1!Find all ODBC drivers
 2while LoopCount <= SQLNumberOfDrivers( 'ODBC' ) do
 3    SetElementAdd(
 4        Setname : sODBCDrivers ,
 5        Elempar : epODBCDriver ,
 6        Newname : SQLDriverName( 'ODBC' , Loopcount) ) ;
 7endwhile ;
 8
 9!First try to find the driver that handles both Access 2007 and up
10!drivers (i.e. .accdb)
11epODBCDriver := first( iOdbcDrivers |
12             FindString(iOdbcDrivers,"*.accdb" ));
13
14!if that does not exist, then try to find the driver that
15!handles .mdb files
16if not epODBCDriver then
17    epODBCDriver := first( iOdbcDrivers |
18                             FindString(iOdbcDrivers,"*.mdb"));
19endif ;
20
21!If we also could not find this driver, we have a problem...
22if not epODBCDriver then
23    raise error "Could not find MS Access ODBC driver!" ;
24endif ;
25
26
27!Now we can create the connection string based on the name of the
28!driver, the name of the database. We provide the DefaultDir as
29!an additional argument to ensure that paths relative to the
30!prj file will work also. If you provide an absolute path as the
31!first argument, the ODBC layer will discard the DefaultDir information.
32ConnectionString := SQLCreateConnectionString(
33    DatabaseInterface              :  'odbc',
34    DriverName                     :  epODBCDriver,
35    DatabaseName                   :  AccessDatabaseFile,
36    AdditionalConnectionParameters :  ";DefaultDir=.\\") ;

If you already know which driver to use as input for the argument DriverName, you can directly use the ConnectionString function.

Example Download

You can import the .ams file into a section of your model. Select an empty Section, and in the menu EditImport... the following .ams file.

CreateAccessFileConnectionString.ams

Note

Please do not forget to close the connection using the intrinsic procedure CloseDataSource() especially when long sessions may occur after data reading.