steinbitur
steinbitur

Reputation: 351

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?

Upvotes: 7

Views: 26392

Answers (4)

Siddharth Rout
Siddharth Rout

Reputation: 149287

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.

Upvotes: 4

topher23
topher23

Reputation: 31

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

Upvotes: 2

brainac
brainac

Reputation: 410

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.

Upvotes: 3

jpw
jpw

Reputation: 44871

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

Upvotes: 10

Related Questions