Get Field Names in External Access Database, VBA Automation
In this article I will explain how you can get the names 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. In the article below I’ve explained this in the article below
You could also get the name of all the tables in the access database. The article below covers this topic:
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 Names, 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 names of the fields 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:
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.