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

4

7

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?

Quevedo answered 4/8, 2013 at 9:19 Comment(0)
S
10

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

Schertz answered 4/8, 2013 at 9:42 Comment(2)
++ Yup that works but that is not the reason for the error message :) See my answer.Vapid
This works in Access365.Deaconry
V
4

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.

Vapid answered 15/3, 2017 at 6:11 Comment(0)
B
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.

Bellina answered 2/1, 2014 at 16:35 Comment(1)
I don't oppose this particular line, but I suggest strongly against the commented philosophy: not having error catching is also less code, leaving screen updating on is less code, etc. It is rare, in vba, where fewer lines of code translates to both a more robust implementation and higher efficiency, and often leads to neither.Lalapalooza
E
2

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

Editorialize answered 3/2, 2014 at 21:8 Comment(1)
Welcome on SO. Unfortunately, it is not every new user which can write good post like you did. Although, you might be interested in those posts on Meta : meta.stackexchange.com/a/114538/181303 and meta.stackexchange.com/questions/12119Bellabelladonna

© 2022 - 2024 — McMap. All rights reserved.