VBA 创建 Access 数据库和表格
Dim db As DAO.Database Dim strDbName As String
' Set the database name strDbName = 'E:\检测设备\力学\TestDatabase.mdb'
' Create a new database Set db = DBEngine.CreateDatabase(strDbName, dbLangGeneral)
' Create a new table definition Dim tdf As TableDef Set tdf = db.CreateTableDef('tableName')
' Define and add fields to the table With tdf .Fields.Append .CreateField('ID', dbLong) .Fields.Append .CreateField('Name', dbText, 50) .Fields.Append .CreateField('Age', dbByte) End With
' Set the primary key and attributes for the ID field With tdf.Fields('ID') .Attributes = dbAutoIncrField .AllowZeroLength = False End With
' Add a primary key index to the ID field tdf.Indexes.Append 'PK_index', dbPrimaryKey, 'ID'
' Add a unique index to the Name field tdf.Indexes.Append 'Name_index', dbIndexUnique, 'Name'
' Save the table definition db.TableDefs.Append tdf
' Close the database db.Close
' Release the database object Set db = Nothing
MsgBox 'New database and table created successfully!'
原文地址: https://www.cveoy.top/t/topic/ofuA 著作权归作者所有。请勿转载和采集!