如何使用VBA创建空白MS Access数据库?

时间:2022-09-27 13:49:41

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:

我是一个尝试使用Excel中的VBA创建空白MS Access数据库的总菜鸟。我想将新数据库命名为“tblImport”。这是我使用的代码:

   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"

“运行时错误3001:应用程序定义或对象定义的错误”

What can I do?

我能做什么?

4 个解决方案

#1


9  

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

CreateDatabase方法中的语言环境常量的名称是错误的:

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

这个:accessApp.DBEngine.CreateDatabase“C:\ tblImport.accdb”,dbLangGenera

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

应该是:accessApp.DBEngine.CreateDatabase“D:\ tblImport.accdb”,DB_LANG_GENERAL

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

改变它,你的代码应该工作。 (它至少对我有用)。

#2


2  

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

老问题,但它对我有用。好像你甚至不需要Access对象。

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

works fine for me.

对我来说很好。

#3


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”为每个语句添加前缀来打开表单,使用DoCmd语句等。例如,我使用TempVars来保存用户数据。我可以使用自动化打开相关的数据库,然后做

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

accessApp.TempVars.Add“CurrentUser”,TempVars.CurrentUser

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

将CurrentUser值从活动数据库传递到新数据库。

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

当你想要做的就是创建一个新的数据库来执行诸如DoCmd.TransferDatabase之类的功能时,你可以使用

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

Access.DBEngine.CreateDatabase“D:\ tblImport.accdb”,DB_LANG_GENERAL

#4


2  

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

老问题。这是我的两分钱。你有一个错字......

dbLangGenera should be dbLangGeneral

dbLangGenera应该是dbLangGeneral

More about it in Workspace.CreateDatabase Method (DAO)

有关它的更多信息,请参阅Workspace.CreateDatabase方法(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.

编辑:一旦帖子关闭,将删除此答案并将其作为评论发布。

#1


9  

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

CreateDatabase方法中的语言环境常量的名称是错误的:

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

这个:accessApp.DBEngine.CreateDatabase“C:\ tblImport.accdb”,dbLangGenera

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

应该是:accessApp.DBEngine.CreateDatabase“D:\ tblImport.accdb”,DB_LANG_GENERAL

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

改变它,你的代码应该工作。 (它至少对我有用)。

#2


2  

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

老问题,但它对我有用。好像你甚至不需要Access对象。

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

works fine for me.

对我来说很好。

#3


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”为每个语句添加前缀来打开表单,使用DoCmd语句等。例如,我使用TempVars来保存用户数据。我可以使用自动化打开相关的数据库,然后做

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

accessApp.TempVars.Add“CurrentUser”,TempVars.CurrentUser

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

将CurrentUser值从活动数据库传递到新数据库。

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

当你想要做的就是创建一个新的数据库来执行诸如DoCmd.TransferDatabase之类的功能时,你可以使用

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

Access.DBEngine.CreateDatabase“D:\ tblImport.accdb”,DB_LANG_GENERAL

#4


2  

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

老问题。这是我的两分钱。你有一个错字......

dbLangGenera should be dbLangGeneral

dbLangGenera应该是dbLangGeneral

More about it in Workspace.CreateDatabase Method (DAO)

有关它的更多信息,请参阅Workspace.CreateDatabase方法(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.

编辑:一旦帖子关闭,将删除此答案并将其作为评论发布。