如何用vb建立一个access数据库
1个回答
展开全部
Dim MyDB As Database, MyWS As Workspace
Dim AuTd As TableDef, TitTd As TableDef, PubTd As TableDef
Dim AuFids(2) As Field, TitFids(5) As Field, PubFids(10) As Field
Dim AuIdx As Index, TitIdx As Index, PubIdx As Index
'´´½¨Êý¾Ý¿âÎļþ¡¢´´½¨±í¡¢´´½¨×ֶδúÂë
Private Sub Command1_Click()
Set MyWS = DBEngine.Workspaces(0)
Set MyDB = MyWS.CreateDatabase("test.mdb", dbLangGeneral, dbVersion03)
Set TitTd = MyDB.CreateTableDef("Titles")
Set AuTd = MyDB.CreateTableDef("Authors")
Set PubTd = MyDB.CreateTableDef("Publishers")
Set AuFids(0) = AuTd.CreateField("AU_ID", dbLong)
AuFids(0).Attributes = dbAutoIncrField
Set AuFids(1) = AuTd.CreateField("Author", dbText)
AuFids(1).Size = 50
AuTd.Fields.Append AuFids(0)
AuTd.Fields.Append AuFids(1)
MyDB.TableDefs.Append AuTd
'ÒÔÏ´úÂëÖ´ÐÐÎÞЧ£¬ÒòΪûÓж¨Òå×Ö¶Î
'MyDB.TableDefs.Append TitTd
'MyDB.TableDefs.Append PubTd
End Sub
Private Sub Command2_Click()
'´´½¨Ë÷Òý²¢ÉèÖÃÊôÏàÖµ
Set AuIdx = AuTd.CreateIndex("AuthorID")
AuIdx.Primary = True
AuIdx.Unique = True
Set NewFld = AuIdx.CreateField("AU_ID")
AuIdx.Fields.Append NewFkl
AuTd.Indexes.Append AuIdx
End Sub
Private Sub Command3_Click()
Dim db As Database
Dim NewTD As TableDef
Dim NewFld As Field
Set db = DBEngine.Workspaces(0).OpenDatabase("test.mdb")
Set NewTD = db.CreateTableDef("student")
Set NewFld = NewTD.CreateField("name", dbInteger)
NewTD.Fields.Append NewFld
db.TableDefs.Append NewTD
db.Close
End Sub
Private Sub Command4_Click()
Dim db As Database
Set db = DBEngine.Workspaces(0).OpenDatabase("test.mdb")
'ÒÔÏÂɾ³ý±í
db.TableDefs.Delete ("Authors")
End Sub
求采纳!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询