用VB建立Access數(shù)據(jù)庫結構
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 " 數(shù)據(jù)庫建立完成!", vbInformation
Exit Sub
Err100: MsgBox "對不起,不能建立表。請先再建表前建立VBEden數(shù)據(jù)庫? ", vbCritical
End Sub
Private Sub cmdCreate_Click()
On Error GoTo Err100
'建立名為 VBEDEN 的數(shù)據(jù)庫 CreateDatabase "VB-CODE", dbLangGeneral
MsgBox "數(shù)據(jù)庫建立完成! ", vbInformation
Exit Sub
Err100: MsgBox "不能建立數(shù)據(jù)庫! " & vbCrLf & vbCrLf & Err.Description, vbInformation
End Sub
建立數(shù)據(jù)庫后,一般用ADO的技術來實現(xiàn)數(shù)據(jù)操作是比較好的辦法。引用微軟的ADO組件,里面兩個對象一個是連接對象用來連接數(shù)據(jù)庫。另一個記錄集對象可以操作數(shù)據(jù)庫。詳細的使用請買本書好好看吧,不是幾句話可以說清的。
用VB建數(shù)據(jù)庫 打開VB后選"外接程序"的第一項 這時打開了一個新的窗體 選"文件"中的"新建"就可以選你要建什么數(shù)據(jù)庫了如:access等 建好數(shù)據(jù)庫后就可以建表了,在表中添加字段就可以了.
|