Import data from Access to Excel (ADO) using VBA in Microsoft Excel

Sub ADOImportFromAccessTable(DBFullName As String, _
    TableName As String, TargetRange As Range)
‘ Example: ADOImportFromAccessTable “C:\FolderName\DataBaseName.mdb”, _
    “TableName”, Range(“C1″)
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
    Set TargetRange = TargetRange.Cells(1, 1)
    ‘ open the database
    Set cn = New ADODB.Connection
    cn.Open “Provider=Microsoft.Jet.OLEDB.4.0; Data Source=” & _
        DBFullName & “;”
    Set rs = New ADODB.Recordset
    With rs
        ‘ open the recordset
        .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable
        ‘ all records
        ‘.Open “SELECT * FROM ” & TableName & _
            ” WHERE [FieldName] = ‘MyCriteria’”, cn, , , adCmdText
        ‘ filter records
       
        RS2WS rs, TargetRange ‘ write data from the recordset to the worksheet
       
‘        ‘ optional approach for Excel 2000 or later (RS2WS is not necessary)
‘        For intColIndex = 0 To rs.Fields.Count – 1 ‘ the field names
‘            TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
‘        Next
‘        TargetRange.Offset(1, 0).CopyFromRecordset rs ‘ the recordset data

    End With
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

A CakePHP ADODB data source driver for MS Access

<?php
/**
 * Include AdoDB files.
 */
App::import(‘Vendor’, ’NewADOConnection’, array(‘file’ => ’adodb’ . DS . ’adodb.inc.php’));

/**
 * Included cake libraries
 */
uses (‘model’ . DS . ’datasources’ . DS . ’dbo’ . DS . ’dbo_adodb’);

/**
 * MS Access AdoDB DBO implementation.
 *
 * Database abstraction implementation for the AdoDB library.
 *
 * @package        cake
 * @subpackage    cake.app.models.datasources.dbo
 */
class DboAdodbAccess extends DboAdodb {
/**
 * Enter description here…
 *
 * @var string
 */
     var $description = ”ADOdb DBO Driver for MS Access”;

   
   /**
 * Returns an array of the fields in the table used by the given model.
 *
 * @param AppModel $model Model object
 * @return array Fields in table. Keys are name and type
 */
    function describe(&$model) {
        $cache = DataSource::describe($model);
        if ($cache != null) {
            return $cache;
        } 

        $fields = false;
        $cols = $this->_adodb->MetaColumns($this->fullTableName($model, false));

        foreach ($cols as $column) {
            $fields[$column->name] = array(
                                        ’type’ => $this->column($column->type),
                                        ’null’ => !$column->not_null,
                                        ’length’ => $column->max_length,
                                    );
            /* extra test otherwise causes ADOFieldObject error - the properties do not exist */
      if (isset($column->has_default) && $column->has_default) {
                $fields[$column->name]['default'] = $column->default_value;
            }
            if (isset($column->primary_key) && $column->primary_key == 1) {
                $fields[$column->name]['key'] = ’primary’;
            }
        }

        $this->__cacheDescription($this->fullTableName($model, false), $fields);
        return $fields;
    }

/**
 * Returns a limit statement in the correct format for the particular database.
 *
 * @param integer $limit Limit of results returned
 * @param integer $offset Offset from which to start results
 * @return string SQL limit/offset statement
 */
    function limit($limit, $offset = null) {
        if ($limit) {
            $rt = ”;
            if (!strpos(strtolower($limit), ’top’) || strpos(strtolower($limit), ’top’) === 0) {
                $rt = ’ TOP’;
            }
            $rt .= ’ ’ . $limit;
            if (is_int($offset) && $offset > 0) {
                $rt .= ’ OFFSET ’ . $offset;
            }
            return $rt;
        }
        return null;
    }

/**
 * Returns a quoted and escaped string of $data for use in an SQL statement.
 *
 * @param string $data String to be prepared for use in an SQL statement
 * @param string $column_type The type of the column into which this data will be inserted
 * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
 * @return string Quoted and escaped data
 */
    function value($data, $column = null, $safe = false) {
        $parent = DboSource::value($data, $column, $safe);
        if ($parent != null) {
            return $parent;
        }

        if ($data === null) {
            return ’NULL’;
        }

        if ($data === ”) {
            return ”””;
        }
    
        switch($column) {
            case ’boolean’:
                $data = $this->boolean((bool)$data);
            break;
            default:
                if (get_magic_quotes_gpc()) {
                    $data = stripslashes(str_replace(“‘”, ”””, $data));
                } else {
                    $data = str_replace(“‘”, ”””, $data);
                }
            break;
        }

        if ((in_array($column, array(‘integer’, ’float’)) && is_numeric($data))
      || (empty($column) && is_numeric($data))) {
            return $data;
        }
        return ”‘” . $data . ”‘”;
    }
  
 /**
 * Builds final SQL statement
 *
 * @param string $type Query type
 * @param array $data Query data
 * @return string
 */
    function renderStatement($type, $data) {
        switch (strtolower($type)) {
            case ’select’:
                extract($data);
                $fields = trim($fields);

                if (strpos($limit, ’TOP’) !== false && strpos($fields, ’DISTINCT ’) === 0) {
                    $limit = ’DISTINCT ’ . trim($limit);
                    $fields = substr($fields, 9);
                }
        return ”SELECT {$limit} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$group} {$order}”;
            break;
            default:
                return DboSource::renderStatement($type, $data);
            break;
        }
    }

/**
 * Removes Identity (primary key) column from update data before returning to parent
 *
 * @param Model $model
 * @param array $fields
 * @param array $values
 * @return array
 */
  function update(&$model, $fields = array(), $values = array()) {
        foreach ($fields as $i => $field) {
            if ($field == $model->primaryKey) {
                unset ($fields[$i]);
                unset ($values[$i]);
                break;
            }
        }
        return DboSource::update($model, $fields, $values);
    }
  
  function buildStatement($query, $model) {
    $join_parentheses = ”;
    $query = array_merge(array(‘offset’ => null, ’joins’ => array()), $query);
        if (!empty($query['joins'])) {
            for ($i = 0; $i < count($query['joins']); $i++) {
                if (is_array($query['joins'][$i])) {
                    $query['joins'][$i] = $this->buildJoinStatement($query['joins'][$i]);
                    if ($i > 0) $join_parentheses = $join_parentheses . ’(‘;
                }
            }
        }
    $join_parentheses = $join_parentheses . ’ ’;
        return $this->renderStatement(’select’, array(
            ’conditions’ => $this->conditions($query['conditions']),
            ’fields’ => join(‘, ’, $query['fields']),
            ’table’ => $join_parentheses . $query['table'],
            ’alias’ => $this->alias . $this->name($query['alias']),
            ’order’ => $this->order($query['order']),
            ’limit’ => $this->limit($query['limit'], $query['offset']),
            ’joins’ => join(‘ ) ’, $query['joins']),
      ’group’ => $this->group($query['group'])
        ));
    } 
  
  function renderJoinStatement($data) {
        extract($data);
    if (empty($type)) {
        return trim(“INNER JOIN {$table} {$alias} ON ({$conditions})”);
      } else {
        return trim(“{$type} JOIN {$table} {$alias} ON ({$conditions})”);
      }
    } 
  
}
?>

IMI ADODB MAPI Address Book Service Provider

IMIADOABP

ADODB –CodeIgniter

Introduction

(Originally discussed here)
Here’s an ADOdb library for use in CI.

What are the benefits of Adodb? It’s mature, and supports lots of databases.
I really like some of the features in it, autoexecute(), and getrow() are particularly useful.

Setup

1. Grab ADODB
2. Unzip to system/application/libraries/adodb/
3. Optionally apply ConfigLoadPatch (as of CI 1.3.3, this is not included), otherwise you’ll have to create a valid config/adodb.php or you’ll get a CI error (if you apply the patch, ADOdb will be able to detect that config/adodb.php doesn’t exist and instead read your config/database.php file).
4. Add the following source code to the appropriate directory

Source

system/application/init/init_adodb.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists(‘_init_adodb_library’)) {
    function _init_adodb_library(&$ci) {
        $db_var = false;
        $debug = false;
        
        // try to load config/adodb.php
        // extra parameter comes from patch at http://www.codeigniter.com/wiki/ConfigLoadPatch/
        // without this patch, if config/adodb.php doesn’t exist, CI will display a fatal error.
        if ($ci->config->load(‘adodb’,true)) {
            $cfg = $ci->config->item(‘adodb’);
            if (isset($cfg['dsn'])) {
                $dsn = $cfg['dsn'];
            }
            
            // set db_var if it’s set in the config file, or false otherwise
            $db_var = isset($cfg['db_var']) && $cfg['db_var'];
            
            $debug = isset($cfg['debug']) && $cfg['debug'];
        }
        
        if (!isset($dsn)) {
            // fallback to using the CI database file
            include(APPPATH.’config/database’.EXT);
            $group = ‘default’;
            $dsn = $db[$group]['dbdriver'].’://’.$db[$group]['username']
                   .’:’.$db[$group]['password'].’@’.$db[$group]['hostname']
                   .’/’.$db[$group]['database'];
        }
        
        // $ci is by reference, refers back to global instance
        $ci->adodb =& ADONewConnection($dsn);
        
        if ($db_var) {
            // also set the normal CI db variable
            $ci->db =& $ci->adodb;
        }
        
        if ($debug) {
            $ci->adodb->debug = true;
        }
    }
}

if ( ! class_exists(‘ADONewConnection’) )
{
     require_once(APPPATH.’libraries/adodb/adodb.inc’.EXT);
}

$obj =& get_instance();
_init_adodb_library($obj);
$obj->ci_is_loaded[] = ‘adodb’;

?>

more