MyTetra Share
Делитесь знаниями!
Как в коде VBA объявить поле Access-таблицы ключевым
17.03.2018
21:40
Текстовые метки: CreateDataBase,PrimaryKey,index,access
Раздел: VBA - Access - CreateDataBase

'Primary Property Example


'This example uses the Primary property to designate a new Index in a new TableDef as the primary Index for that table.

'Note that setting the Primary property to True automatically sets Unique and Required properties to True as well.


Sub PrimaryX() 'oDb)


Dim dbsNorthwind As DAO.Database

Dim tdfNew As DAO.TableDef

Dim idxNew As DAO.Index

Dim idxLoop As DAO.Index

Dim fldLoop As DAO.Field

Dim prpLoop As DAO.Property


Set dbsNorthwind = OpenDatabase("\\rudh002113\share_\mdb\21_MonitoringSkidReader.accdb") 'oDb 'OpenDatabase("Northwind.mdb")


' Create and append a new TableDef object to the

' TableDefs collection of the Northwind database.

Set tdfNew = dbsNorthwind.CreateTableDef("NewTable")

tdfNew.Fields.Append tdfNew.CreateField("NumField", dbLong, 20)

tdfNew.Fields.Append tdfNew.CreateField("TextField", dbText, 20)

dbsNorthwind.TableDefs.Append tdfNew


With tdfNew

' Create and append a new Index object to the

' Indexes collection of the new TableDef object.

Set idxNew = .CreateIndex("NumIndex")

idxNew.Fields.Append idxNew.CreateField("NumField")

idxNew.Primary = True

.Indexes.Append idxNew

Set idxNew = .CreateIndex("TextIndex")

idxNew.Fields.Append idxNew.CreateField("TextField")

' idxNew.Primary = True

.Indexes.Append idxNew


Debug.Print .Indexes.Count & " Indexes in " & _

.Name & " TableDef"


' Enumerate Indexes collection.

For Each idxLoop In .Indexes


With idxLoop

Debug.Print " " & .Name


' Enumerate Fields collection of each Index

' object.

Debug.Print " Fields"

For Each fldLoop In .Fields

Debug.Print " " & fldLoop.Name

Next fldLoop


' Enumerate Properties collection of each

' Index object.

Debug.Print " Properties"

For Each prpLoop In .Properties

Debug.Print " " & prpLoop.Name & _

" = " & IIf(prpLoop = "", "[empty]", _

prpLoop)

Next prpLoop

End With


Next idxLoop


End With


dbsNorthwind.TableDefs.Delete tdfNew.Name

dbsNorthwind.Close


End Sub

Так же в этом разделе:
 
MyTetra Share v.0.52
Яндекс индекс цитирования