|
|
Создание Таблицы (DAO) - Список дней недели
Простейший пример кода, который создает таблицу дней недели ("tempWeekDays") с порядковым номером в поле "DayID" и названием дня в поле "DayName"
Private Sub CreateWeekDaysTable()
Const strTableName As String = "tempWeekDays"
Dim tbl As TableDef
Dim idx As Index
Dim fld As Field
Dim rst As Recordset
Dim i As Integer
On Error Resume Next
CurrentDb.TableDefs.Delete strTableName
Err.Clear
On Error GoTo CreateWeekDaysTableErr
Set tbl = CurrentDb.CreateTableDef(strTableName)
With tbl
.Fields.Append tbl.CreateField("DayID", dbLong)
.Fields.Append tbl.CreateField("DayName", dbText, 20)
Set idx = .CreateIndex("Primary Key")
With idx
.Fields.Append .CreateField("DayID")
.Unique = True
.Primary = True
End With
.Indexes.Append idx
End With
CurrentDb.TableDefs.Append tbl
Set rst = CurrentDb.OpenRecordset(strTableName, dbOpenDynaset)
With rst
For i = 1 To 7
.AddNew
!DayID = i
!DayName = DayName(i)
.Update
Next i
End With
CreateWeekDaysTableBye:
On Error Resume Next
Set idx = Nothing
Set tbl = Nothing
rst.Close
Set rst = Nothing
Exit Sub
CreateWeekDaysTableErr:
MsgBox "Произошла ошибка при выполнении процедуры " & _
"[CreateWeekDaysTable] :" & vbCrLf & _
Err.Description & vbCrLf & _
"Номер ошибки = " & Err.Number, vbCritical
Resume CreateWeekDaysTableBye
End Sub
Private Function DayName(DayNo As Integer) As String
On Error GoTo DayNameErr
Select Case DayNo
Case 1: DayName = "Понедельник"
Case 2: DayName = "Вторник"
Case 3: DayName = "Среда"
Case 4: DayName = "Четверг"
Case 5: DayName = "Пятница"
Case 6: DayName = "Суббота"
Case 7: DayName = "Воскресенье"
End Select
DayNameBye: Exit Function
DayNameErr: DayName = "#Err
|
|