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

23,193

Solution 1

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).

Solution 2

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.

Solution 3

Old question, but it was useful for me. Seems like you don't even need Access object.

Access.DBEngine.CreateDatabase "D:\tblImport.accdb", DB_LANG_GENERAL

works fine for me.

Solution 4

You should use the first method if you intend to use the automation object you've created. You can open forms, use DoCmd statements, etc., simply by prefacing each statement with "accessApp." For example, I use TempVars to hold user data. I can open a related database using automation, then do

accessApp.TempVars.Add "CurrentUser", TempVars.CurrentUser

to pass the CurrentUser value from the active database to the new database.

When all you want to do is create a new database to do a function such as DoCmd.TransferDatabase on, you can just use

Access.DBEngine.CreateDatabase "D:\tblImport.accdb", DB_LANG_GENERAL

Share:
23,193
steinbitur
Author by

steinbitur

Updated on February 20, 2021

Comments

  • steinbitur
    steinbitur about 3 years

    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?