Convidad 22/7/2011, 19:32
outro
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim tblNewTable As DAO.TableDef
Dim fldNewField As DAO.Field
Dim idxNewIndex As DAO.index
Dim fldIndexField As DAO.Field
so the first thing to do is create a workspace then open the database you want to modify in the ws.
Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase(strDatabase) ' strDatabase is the full path to the accdb or mdb
next thing to do is create a table, in fact we will first create a tabledef, then create some fields in it then append the table to the tables collection (to make it permanent) Set tblNewTable = db.TableDefs(strTableName) ' you will need to declare tblNewTable As DAO.TableDef at the top of your procedure
strFieldname = "ID"
Set fldNewField = tblNewTable.CreateField(strFieldname, dbLong) ' let's create an autonumber first
With fldNewField
' Appending dbAutoIncrField to Attributes
' tells Jet that it's an Autonumber field
.Attributes = .Attributes Or dbAutoIncrField
End With
' now a long integer but most others are the same
strFieldname = "Long_Integer_Field"
Set fldNewField = tblNewTable.CreateField(strFieldname, dbLong)
' or you can use dbInteger, dbSingle, dbDate, dbCurrency, dbBoolean, dbBinary dbMemo or, dbLongBinary (did I miss any?)
' after each field you need to append it to the table def
tblNewTable.Fields.Append fldNewField
' text is a little different, you need to specify the length
Set fldNewField = tblNewTable.CreateField(strFieldname, dbText, intFieldLength)
tblNewTable.Fields.Append fldNewField
' once you've added all the fields then we need to append the table to the tables collection
db.TableDefs.Append tblNewTable There's a table and all the fields now for the indexes - here are some samples ' PrimaryKey"
Set idxNewIndex = tblNewTable.CreateIndex("PrimaryKey")
Set fldIndexField = idxNewIndex.CreateField(strFieldname)
idxNewIndex.Primary = True
idxNewIndex.Fields.Append fldIndexField
tblNewTable.Indexes.Append idxNewIndex
' Unique"
Set idxNewIndex = tblNewTable.CreateIndex(strIndexName)
Set fldIndexField = idxNewIndex.CreateField(strFieldname)
idxNewIndex.Unique = True
idxNewIndex.Fields.Append fldIndexField
tblNewTable.Indexes.Append idxNewIndex
' NonUnique"
Set idxNewIndex = tblNewTable.CreateIndex(strIndexName)
Set fldIndexField = idxNewIndex.CreateField(strFieldname)
idxNewIndex.Fields.Append fldIndexField
tblNewTable.Indexes.Append idxNewIndex