Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a blank MS Access database be created using VBA?

I'm a total noob trying to create a blank MS Access database using VBA in Excel. I want to name the new database "tblImport". This is the code I´m using:

   Sub makedb()
   Dim accessApp As Access.Application
   Set accessApp = New Access.Application
   accessApp.DBEngine.CreateDatabase "C:\tblImport.accdb", dbLangGenera
   accessApp.Quit
   Set accessApp = Nothing
   End Sub

I get the following error message:

"Run Time Error 3001: Application Defined or Object Defined Error"

What can I do?

like image 570
steinbitur Avatar asked Aug 04 '13 09:08

steinbitur


People also ask

What are the four steps to create a blank database?

Step 1 : Start by opening MS – Access. Step 2 : Select Blank Database. Step 3 : In the file name field enter a name for the database. Step 4 : Clickl Create.

What are the three ways to create a database in Access?

Way 1: Create Access database with templates. Way 2: Create a blank Access database with “Blank database” or “Blank Web database” command. Way 3: Import existing data into Microsoft Access and save as Access database.


2 Answers

The name of the locale constant in the CreateDatabase method is wrong:

This:
accessApp.DBEngine.CreateDatabase "C:\tblImport.accdb", dbLangGenera

Should be:
accessApp.DBEngine.CreateDatabase "D:\tblImport.accdb", DB_LANG_GENERAL

Change that and your code should work. (It does for me at least).

like image 185
jpw Avatar answered Sep 28 '22 04:09

jpw


Old Question. Here are my two cents. You have a typo...

dbLangGenera should be dbLangGeneral

More about it in Workspace.CreateDatabase Method (DAO)

Voting to close this question as per Dealing with questions with obvious replies

Try this. This works.

Sub makedb()
    Dim accessApp As Access.Application
    Set accessApp = New Access.Application

    accessApp.DBEngine.CreateDatabase "C:\tblImport.accdb", dbLangGeneral
    accessApp.Quit

    Set accessApp = Nothing
End Sub

EDIT: Will delete this answer and post it as a comment once the post is closed.

like image 25
Siddharth Rout Avatar answered Sep 28 '22 02:09

Siddharth Rout