Option Explicit
Public dbs
As Database
Private tdf
As TableDef
Private qdf
As QueryDef
Private fld
As Field
Private rst
As Recordset
Private idx
As Index
Private Sub Command1_Click()
Dim i
As Integer
'Открытие БД
Set dbs = OpenDatabase(App.Path & "\" & myDatabase.mdb)
'Проверка: м.б. БД уже конвертирована?
On Error GoTo ErrH
Set rst = dbs.OpenRecordset("SELECT DiaryID FROM tabl_Diary"
MsgBox "Эта база данных уже конвертирована"
GoTo Fin
Nxt1:
On Error GoTo 0
Set rst =
Nothing
'########################################################################################
' ДНЕВНИК
'########################################################################################
'Создаем индексированное поле
Set tdf = dbs.TableDefs("tabl_Diary"
Set idx = tdf.CreateIndex("NewIndex"
tdf.Fields.Append tdf.CreateField("
iaryID", dbLong)
idx.Fields.Append idx.CreateField("
iaryID"
idx.Unique =
True
'Заполняем индексированное поле
Set rst = dbs.OpenRecordset("SELECT DiaryID FROM tabl_Diary"
i = rst.RecordCount
If i = 0
Then
GoTo FinDiary
Else
i = 0
rst.MoveFirst
Do
i = i + 1
rst.Edit
rst.Fields("
iaryID"
.Value = i
rst.Update
rst.MoveNext
Loop Until rst.EOF
End If
FinDiary:
'Закрываем рекордсет
rst.Close
Set rst =
Nothing
'Свойства поля
Set fld = tdf.Fields("
iaryID"
fld.DefaultValue = 0
fld.ValidationRule = "
Not Is Null And >0"
idx.Primary =
True
tdf.Indexes.Append idx
tdf.Indexes.Refresh
'Заменяем запрос
dbs.QueryDefs.Delete "Q_Diary"
dbs.QueryDefs.Refresh
Set qdf = dbs.CreateQueryDef("Q_Diary", "SELECT DiaryID,DateOfRecord,NoteCaption,NoteTxt,ToRprt,KeyWords From tabl_Diary ORDER BY DateOfRecord, NoteCaption"
' dbs.QueryDefs.Append qdf
dbs.QueryDefs.Refresh
'########################################################################################
' ЦИТАТЫ
'########################################################################################
'Подменяем таблицу
dbs.Execute ("SELECT AuthorName,Theme,SourceCyt,TextCyt INTO tmp1 FROM tabl_Cytates ORDER BY CytateID"
dbs.TableDefs.Delete "tabl_Cytates"
dbs.TableDefs.Refresh
dbs.TableDefs("tmp1"
.
Name = "tabl_Cytates"
Set tdf = dbs.TableDefs("tabl_Cytates"
'Создаем индексированное поле
Set idx = tdf.CreateIndex("CytIndex"
tdf.Fields.Append tdf.CreateField("CytateID", dbLong)
idx.Fields.Append idx.CreateField("CytateID"
idx.Unique =
True
'Заполняем индексированное поле
Set rst = dbs.OpenRecordset("SELECT CytateID FROM tabl_Cytates"
i = rst.RecordCount
If i = 0
Then
GoTo FinCyt
Else
i = 0
rst.MoveFirst
Do
i = i + 1
rst.Edit
rst.Fields("CytateID"
.Value = i
rst.Update
rst.MoveNext
Loop Until rst.EOF
End If
FinCyt:
'Закрываем рекордсет
rst.Close
Set rst =
Nothing
'Свойства поля
Set fld = tdf.Fields("CytateID"
fld.DefaultValue = 0
fld.ValidationRule = "
Not Is Null And >0"
idx.Primary =
True
tdf.Indexes.Append idx
tdf.Indexes.Refresh
'########################################################################################
' ЗАВЕРШЕНИЕ РАБОТЫ
'########################################################################################
MsgBox "Конвертирование выполнено успешно!"
Fin:
dbs.Close
Set dbs =
Nothing
Exit Sub
ErrH:
Resume Nxt1
End Sub