|
|
Добавление поля в таблицу (DAO) + Создание ключа (Счетчик)
Private Sub AddNewKeyField(sTName$, sFiedName$)
Dim dbs As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim idx As DAO.Index
Dim idxFLD As DAO.Field
On Error GoTo AddNewFieldErr
Set dbs = CurrentDb
Set tbl = dbs.TableDefs(sTName$)
Set fld = tbl.CreateField(sFiedName, dbLong)
fld.Attributes = fld.Attributes + dbAutoIncrField
fld.OrdinalPosition = 0
tbl.Fields.Append fld
Set idx = tbl.CreateIndex("PrimaryKey")
With idx
.Fields.Append .CreateField(sFiedName)
.Unique = True
.Primary = True
End With
tbl.Indexes.Append idx
tbl.Indexes.Refresh
AddNewFieldBye:
On Error Resume Next
Set idx = Nothing
Set fld = Nothing
Set tbl = Nothing
Set dbs = Nothing
Exit Sub
AddNewFieldErr:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
"in procedure AddNewField of Module mod00Test", vbCritical, "Error!"
Resume AddNewFieldBye
End Sub
|
|