生徒マスターテーブルの作成
作成した図書貸出テーブルです、[図書ID]は図書マスターと、[生徒ID]は生徒マスターとリレーションシップで接続され使用します。
VBAコード
下記VBAコードに変更してください。
Private Sub CommandButton1_Click()
Dim spath As String
spath = SelectFolder_FileDialog(ActiveWorkbook.Path)
If spath = "" Then
Exit Sub
End If
If Right(spath, 1) <> "\" Then
spath = spath & "\"
End If
'データベースとテーブルの作成
MyMakeTosyoTable spath
End Sub
下記VBAコードに変更してください。
'データベースとテーブルの作成
Private Sub MyMakeTosyoTable(sDir As String)
Dim db As Database
Dim tbdef As TableDef
Dim fld As Field
Dim idx As DAO.Index
' データベースを作成します
Set db = DBEngine.Workspaces(0).CreateDatabase(sDir & "図書データベース.mdb", dbLangJapanese)
'図書マスターテーブルを作成します
Set tbdef = db.CreateTableDef("図書マスター")
'フィールドを作成します。
Set fld = tbdef.CreateField("図書ID", dbLong)
'オートナンバー型にします。
fld.Attributes = dbAutoIncrField
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("分類", dbText, 20)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("署名", dbText, 100)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("著書名", dbText, 30)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("出版社", dbText, 30)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("発行年月日", dbDate)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("ISBN番号", dbText, 20)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("値段", dbCurrency)
tbdef.Fields.Append fld
'主キーの作成
Set idx = tbdef.CreateIndex("PrimaryKey")
Set fld = idx.CreateField("図書ID", dbLong)
idx.Fields.Append fld
'Primaryプロパティをセット
idx.Primary = True
'インデックスを追加
tbdef.Indexes.Append idx
db.TableDefs.Append tbdef
'終了処理を行います
Set idx = Nothing
Set fld = Nothing
Set tbdef = Nothing
'生徒マスターテーブルを作成します
Set tbdef = db.CreateTableDef("生徒マスター")
Set fld = tbdef.CreateField("生徒ID", dbLong)
fld.Attributes = dbAutoIncrField
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("年", dbLong)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("組", dbLong)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("番号", dbLong)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("名前", dbText, 30)
tbdef.Fields.Append fld
Set idx = tbdef.CreateIndex("PrimaryKey")
Set fld = idx.CreateField("生徒ID", dbLong)
idx.Fields.Append fld
idx.Primary = True
tbdef.Indexes.Append idx
db.TableDefs.Append tbdef
'終了処理を行います
Set idx = Nothing
Set fld = Nothing
Set tbdef = Nothing
'貸出データテーブルを作成します
Set tbdef = db.CreateTableDef("貸出データ")
Set fld = tbdef.CreateField("貸出ID", dbLong)
fld.Attributes = dbAutoIncrField
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("図書ID", dbLong)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("生徒ID", dbLong)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("貸出日", dbDate)
tbdef.Fields.Append fld
Set fld = tbdef.CreateField("返却日", dbDate)
tbdef.Fields.Append fld
Set idx = tbdef.CreateIndex("PrimaryKey")
Set fld = idx.CreateField("貸出ID", dbLong)
idx.Fields.Append fld
idx.Primary = True
tbdef.Indexes.Append idx
db.TableDefs.Append tbdef
'終了処理を行います
Set idx = Nothing
Set fld = Nothing
Set tbdef = Nothing
'データベースを閉じます
db.Close
'終了処理を行います
Set db = Nothing
End Sub