Data Access Object (DAO) and Visual Basic.NET to create an Access database interrogation tool

Problem

The institution I work with receives data through a variety of vehicles and since they conduct different surveys and investigations the data does not always fit into a single model. Once the data is received, I need to get the data loaded into SQL Server. One of the common vehicles used to deliver the data is Microsoft Access 2007. I spend a significant amount of time looking through these databases to figure out how the data is arranged and would like to find a way of interrogating the database in a more efficient manner.

Solution

Evaluating an Access database manually can be quite time consuming, particularly when there is a large number of tables and queries. Luckily you can create a tool that will assist with this by using either Visual Studio or Visual Studio Express, some .NET Framework references, and COM objects. The examples used here will be using Visual Basic, but the same can be accomplished using C#.

The Microsoft Office 2007 Access Database Engine Object

There are a few COM objects that interact with Microsoft Access, but the relevant one that can dive into the innerds of Access is the Microsoft Office 2007 Access Database Engine Object. This object took the place of DAO libraries in Office 2007 and is specific to Access 2007; if you want to obtain information on previous versions of Access then you should use one of the previous DAO libraries (like the Microsoft DAO 3.6 Object Library). My assumption when creating the evaluation application was that I want to view the properties and structure of the database, tables, queries, and relationships within Access 2007 databases. This database engine can be installed as a redistributable with applications that interact with other Office products. It also provides the ability to import Excel 2007 spreadsheets using SSIS and the Import/Export Wizard as mentioned in a previous tip.

Access houses information on its tables in a TableDefs object, which houses individual TableDef objects for each table. The TableDef has a number of methods and properties available, but of most importance for this project is:

Property/MethodDescription
NameThe name of the table. The TableDef collection of tables also includes system tables within Access, so may have to filter them out

AttributesAttributes of the table stored as bitwise operation

FieldsThe fields that reside within the table. This collection also has properties and methods that we will discuss soon

PropertiesA collection of table properties

RecordCountA count of rows in the table. If this table is a linked table then this value will be -1

ValidationRuleAny restriction on the values entered (domain)

ValidationTextThe text to be displayed when the data entered does not follow the validation rule

Information on queries in Access databases are housed in the QueryDefs object. Within QueryDefs is a collection of QueryDef objects similar to the TableDef objects in the TableDefs collection. The properties I will use to find information on the queries are:

Property/MethodDescription
NameThe name of the query

TypeThe type refers to whether it is a SELECT, APPEND (insert), UPDATE, or PASS-THROUGH. An integer is returned.

SQLThe actual SQL statement of the query

ParametersA collection of parameters that the query will accept

FieldsA collection of columns/fields returned by the query

Relationships between tables in Access are stored in a Relations object. Below are the areas of the Relations object I will use to obtain information on relationships:

Property/MethodDescription
NameThe name of the relationship

TableThe table being referenced by the relationship-the parent

ForeignTableThe child table in the relationship

FieldsThe columns that define the relationship

Creating the Evaluation Tool

The first step in setting up the project is to create a project using the Visual Studio product of your choice. Once Visual Studio is open click File and choose New Project:

Creating the Evaluation Tool

Chances are that you will want to do more with the data than just view it or present it in a logical and user-friendly format. In this project I chose a Windows form application:

you will want to do more with the data than just view it or present it in a logical and user-friendly format

The next step is to add the necessary references to the project. That is accomplished by right-clicking the Solution in the Solution Explorer pane and choosing Add Reference:

add the necessary references to the project

A set of references are automatically added to the project:

a set of references are automatically added to the project

There is another reference that should be added to the project. If using Microsoft Access 2007 then add C:\Program Files\Microsoft Office\Office12\ACEDAO.DLL by clicking the Browse tab, navigating to the appropriate folder, and then double-clicking the file. If using Access 2003 add the Microsoft Data Access Objects (DAO) 3.6 Object Library using the COM tab:

using Microsoft Access 2007 then add C:\Program Files\Microsoft Office\Office12\ACEDAO.DLL

 using Access 2003 add the Microsoft Data Access Objects (DAO) 3.6 Object Library using the COM tab

The Code to Access the Database

Once the reference is added you can access it using the namespace or you can use an alias to import the namespace. For example, if you want to reference the Microsoft.Office.Interop.Access.Dao namespace as MX then at the very top of the page type Imports MX = Microsoft.Office.Interop.Access.Dao. I decided to have a series of TreeViews to represent the tables, queries, and relationships. Once you have the form available you can add an OnClick event on a button. In the code I declare a new DBEngine and use it to open the Access database. In this project I want to find out everything I can about the database, so we start by gathering the database properties into a StringBuilder and displaying it in a textbox. The next step is that I loop through the items in each collection, obtaining the desired information.

The Code to Access the Database

In the Data Viewer section I had to be a little more creative. I built a data table using the fields represented and used the New Row function to populate the data table. Once the table was fully populated I set the datasource of a DataGridView:

build a data table using the fields represented and use the New Row function to populate the data table

Next Steps

Leave a Reply

Your email address will not be published. Required fields are marked *