如何在预先存在的 table 和 VBA 中为 MS Access 创建一个字段?
How to create a field in in a preexisting table with VBA for MS Access?
我有一个数据库 Database
和一个 table Table
。我在网上找到了如何从新创建的数据库和 table 在 MS Access 的 VBA 中创建字段,但是当数据库和 table 已经存在时我不知道如何执行此操作之前的宏已经运行。
要在没有预先存在的数据库和 table 的情况下执行此操作,我可以 运行 以下操作:
Sub CreateTable()
Dim db As DAO.Database
Dim table1 As DAO.TableDef
Set db = CurrentDb
Set table1 = db.CreateTableDef("ExampleTable")
With table1
.Fields.Append.CreateField("newField", String)
End With
但是我如何调整它以将相同的字段添加到预先存在的 table?
或者更具体地说,我该如何修改
Set table1 = db.CreateTableDef("ExampleTable")
以便 table1
指向 db
数据库中现有的 table?
像这样。只需在 TableDefs
集合中引用 table 名称,而不是创建一个。
Public Sub CreateTable()
Dim table1 As DAO.TableDef
Dim fld As DAO.Field
Dim tdf As DAO.TableDef
' Create the new Table Def object
Set table1 = New DAO.TableDef
' Name the Table
table1.Name = "Test"
' Create the new Field
Set fld = New DAO.Field
fld.Name = "newField"
fld.Type = DataTypeEnum.dbText
' Append the Field to the table
table1.Fields.Append fld
' Append the table to the Table Def Collection
' Without this the table just lives in memory
CurrentDb.TableDefs.Append table1
' Create another Field
Set fld = New DAO.Field
fld.Name = "newField2"
fld.Type = DataTypeEnum.dbText
' Append the New Field to the EXISTING table
CurrentDb.TableDefs("Test").Fields.Append fld
End Sub
您可以通过执行 Access DDL 语句向现有 table 添加字段。
Dim strDdl As String
strDdl = "ALTER TABLE ExampleTable ADD COLUMN newField TEXT(255);"
CurrentProject.Connection.Execute strDdl
在TEXT(255)
中,255是字段大小---该字段可以包含的最大字符数。 255 是 Access 文本字段的绝对上限。从 ADODB.Connection.Execute
方法创建 TEXT
字段时,必须包含字段大小的值。如果您愿意,请选择较小的值。
当您使用 DAO 方法创建新字段时,如您的问题所示,文本字段的字段大小是可选的。但是,当您未指定大小时,Access 会使用 Access 选项中的 "Default text field size" 设置。
我有一个数据库 Database
和一个 table Table
。我在网上找到了如何从新创建的数据库和 table 在 MS Access 的 VBA 中创建字段,但是当数据库和 table 已经存在时我不知道如何执行此操作之前的宏已经运行。
要在没有预先存在的数据库和 table 的情况下执行此操作,我可以 运行 以下操作:
Sub CreateTable()
Dim db As DAO.Database
Dim table1 As DAO.TableDef
Set db = CurrentDb
Set table1 = db.CreateTableDef("ExampleTable")
With table1
.Fields.Append.CreateField("newField", String)
End With
但是我如何调整它以将相同的字段添加到预先存在的 table?
或者更具体地说,我该如何修改
Set table1 = db.CreateTableDef("ExampleTable")
以便 table1
指向 db
数据库中现有的 table?
像这样。只需在 TableDefs
集合中引用 table 名称,而不是创建一个。
Public Sub CreateTable()
Dim table1 As DAO.TableDef
Dim fld As DAO.Field
Dim tdf As DAO.TableDef
' Create the new Table Def object
Set table1 = New DAO.TableDef
' Name the Table
table1.Name = "Test"
' Create the new Field
Set fld = New DAO.Field
fld.Name = "newField"
fld.Type = DataTypeEnum.dbText
' Append the Field to the table
table1.Fields.Append fld
' Append the table to the Table Def Collection
' Without this the table just lives in memory
CurrentDb.TableDefs.Append table1
' Create another Field
Set fld = New DAO.Field
fld.Name = "newField2"
fld.Type = DataTypeEnum.dbText
' Append the New Field to the EXISTING table
CurrentDb.TableDefs("Test").Fields.Append fld
End Sub
您可以通过执行 Access DDL 语句向现有 table 添加字段。
Dim strDdl As String
strDdl = "ALTER TABLE ExampleTable ADD COLUMN newField TEXT(255);"
CurrentProject.Connection.Execute strDdl
在TEXT(255)
中,255是字段大小---该字段可以包含的最大字符数。 255 是 Access 文本字段的绝对上限。从 ADODB.Connection.Execute
方法创建 TEXT
字段时,必须包含字段大小的值。如果您愿意,请选择较小的值。
当您使用 DAO 方法创建新字段时,如您的问题所示,文本字段的字段大小是可选的。但是,当您未指定大小时,Access 会使用 Access 选项中的 "Default text field size" 设置。