简体   繁体   English

如何在 MS-Access 中使用 VBA 不在 currentdb 中创建表

[英]How do I create a table in MS-Access using VBA NOT in currentdb

First time user here.第一次来这里的用户。 I need to create a new table in Access using VBA but NOT in the currentdb.我需要使用 VBA 在 Access 中创建一个新表,但不在 currentdb 中。 How can I accomplish this?我怎样才能做到这一点?

Thanks, Rimuru谢谢, 利姆鲁

There are two ways that you can do this.有两种方法可以做到这一点。

Firstly, you can use SQL to do this:首先,您可以使用 SQL 来执行此操作:

Sub sCreateTable1()
    Dim db As DAO.Database
    Set db = DBEngine(0).OpenDatabase("J:\downloads\test.accdb")
    db.Execute "CREATE TABLE [test1] (Field1 CHAR);"
    db.Close
    Set db = Nothing
End Sub

Secondly, you can use DAO to do this:其次,您可以使用 DAO 来执行此操作:

Sub sCreateTable2()
    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Set db = DBEngine(0).OpenDatabase("J:\downloads\test.accdb")
    Set tdf = db.CreateTableDef("test2")
    Set fld = tdf.CreateField("Field1", dbText, 100)
    tdf.Fields.Append fld
    db.TableDefs.Append tdf
    Set fld = Nothing
    Set tdf = Nothing
    db.Close
    Set db = Nothing
End Sub

Regards,问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM