用VB建立Access数据库结构
【打印文章】
Private Sub Command1_Click()
On Error GoTo Err100
注释:定义表与字段
Dim DefDatabase As Database
Dim DefTable As TableDef, DefField As Field
Set DefDatabase = Workspaces(0).OpenDatabase(App.Path & "\vbeden.mdb", 0, False)
Set DefTable = DefDatabase.CreateTableDef("VB编程乐园")
注释:dbBinary = 9
注释:dbBoolean = 1
注释:dbByte = 2
注释:dbChar=18
注释:dbDate=8
注释:dbInteger=3
注释:dbLong=4
注释:dbMemo=12
注释:dbText=10
注释:建立Name字段为8个字符型
Set DefField = DefTable.CreateField("Name", dbText, 8)
DefTable.Fields.Append DefField
Set DefField = DefTable.CreateField("Sex", dbText, 2)
DefTable.Fields.Append DefField
注释:该字段允许为空
DefField.AllowZeroLength = True
注释:建立Age字段为3个的常整型
Set DefField = DefTable.CreateField("Age", dbInteger, 3)
注释:字段追加
DefTable.Fields.Append DefField
注释:表追加
DefDatabase.TableDefs.Append DefTable
MsgBox " VB编程乐园数据库建立完成!", vbInformation
Exit Sub
Err100:
MsgBox "对不起,不能建立表。请先再建表前建立VBEden数据库? ", vbCritical
End Sub
Private Sub cmdCreate_Click()
On Error GoTo Err100
注释:建立名为 VBEDEN 的数据库
CreateDatabase "VB-CODE", dbLangGeneral
MsgBox "数据库建立完成! ", vbInformation
Exit Sub
Err100:
MsgBox "不能建立数据库! " & vbCrLf & vbCrLf & Err.Description, vbInformation
End Sub
On Error GoTo Err100
注释:定义表与字段
Dim DefDatabase As Database
Dim DefTable As TableDef, DefField As Field
Set DefDatabase = Workspaces(0).OpenDatabase(App.Path & "\vbeden.mdb", 0, False)
Set DefTable = DefDatabase.CreateTableDef("VB编程乐园")
注释:dbBinary = 9
注释:dbBoolean = 1
注释:dbByte = 2
注释:dbChar=18
注释:dbDate=8
注释:dbInteger=3
注释:dbLong=4
注释:dbMemo=12
注释:dbText=10
注释:建立Name字段为8个字符型
Set DefField = DefTable.CreateField("Name", dbText, 8)
DefTable.Fields.Append DefField
Set DefField = DefTable.CreateField("Sex", dbText, 2)
DefTable.Fields.Append DefField
注释:该字段允许为空
DefField.AllowZeroLength = True
注释:建立Age字段为3个的常整型
Set DefField = DefTable.CreateField("Age", dbInteger, 3)
注释:字段追加
DefTable.Fields.Append DefField
注释:表追加
DefDatabase.TableDefs.Append DefTable
MsgBox " VB编程乐园数据库建立完成!", vbInformation
Exit Sub
Err100:
MsgBox "对不起,不能建立表。请先再建表前建立VBEden数据库? ", vbCritical
End Sub
Private Sub cmdCreate_Click()
On Error GoTo Err100
注释:建立名为 VBEDEN 的数据库
CreateDatabase "VB-CODE", dbLangGeneral
MsgBox "数据库建立完成! ", vbInformation
Exit Sub
Err100:
MsgBox "不能建立数据库! " & vbCrLf & vbCrLf & Err.Description, vbInformation
End Sub
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】