MyTetra Share
Делитесь знаниями!
Как в коде VBA объявить поле Access-таблицы ключевым
Время создания: 16.03.2019 23:43
Текстовые метки: CreateDataBase, PrimaryKey, index, access
Раздел: Разные закладки - VBA - Access - CreateDataBase
Запись: xintrea/mytetra_db_adgaver_new/master/base/151377558287c4p3stt2/text.html на raw.githubusercontent.com

'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("strPath.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.65
Яндекс индекс цитирования