Get Field Types in External Access Database, VBA Automation,
In this article I will explain how you can get the types of all the fields in a table in an external access database using VBA.
The first step would be to connect to an external database.I’ve explained this in the article below
Note: The examples in this article will be provided for 2 methods of automation:
- Late Binding
- Early binding
In the latter method we add reference to the Access Object Library, before execution. It will run faster and we will have access to the VBA editor intellisense. On the other hand there is always the risk of compatibility issues arising when the program is run on a computer with a different version of Access installed. For more information about early vs late binding please see the link below:
Note: Although the code in this article was written in the Excel VBA editor, it can be used in any office application with VBA.
Get Field Types, In External Access Database:
The codes below are based on the following assumptions:
- The access database is located in the path “D:StuffBusinessTemp”.
- The name of the access database is “NewDB.accdb”.
- It is assumed the database has a table with the name “MyTable1”.
- The table has 3 fields with the names, “MyField1”, “MyField2”, “MyField3”
The code below will print the type of all the fields in the table “MyTable1” in column A of the current sheet:
In early binding you will need to add reference to the libraries below. This can be done in the VBA editor (Tools>>Reference):
- Microsoft Access Access Object Library
- Microsoft ADO Ext. for DDL and Security
Note: The versions of the libraries mentioned above installed on my computer might be different than what is installed on your computer. This will not effect the code:
These numbers can then be compared with the
enumeration to figure out exactly what type of data they are. For example:
- The value 3 is an
- The value 202 is an
You can download the file and code used in this article from the link below:
Note: Although the code was written in the Excel VBA Editor, it can be written in any other office application.
- Get Field Names in External Access Database, VBA Automation
- Check If Table Exists in External Access Database, VBA Automation
- VBA Automation, External Access Database Get Table Names