VBA Access Create Table in External Database (Automation)
In this article I will explain how you can create a table in an external access database using VBA.
Before creating a table you will need to connect to an external database. You could either create a blank access database using the topic covered in the article below:
Or you could connect to an existing database using the topic covered in the article below:
Note: The examples in this article will be provided for 2 methods of automation:
- Early binding
- Late binding
In the first 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.
Creating Table, In External Access Database:
The example codes assume there is an access database located in the path “D:StuffBusinessTemp” under the name “NewDB.accdb”. They will create a table with the name “MyTable”.
Note: I have the Microsoft Access 14.0 Object Library installed on my computer. There might be a different version installed the computer you are using. This will not affect the result of the code.
Note: Attempting to create a table that already exists will result in a runtime error. A good idea would be to check if the table exists before trying to create it. This has been covered in the article below:
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.