空手建立Access數(shù)據(jù)庫(kù)

字號(hào):

程序圖例
    -------------------------------------
    軟件說(shuō)明:包含源碼
    說(shuō)明: frmMain表單一個(gè)
    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 & "\VB-CODE.MDB", 0, False)
    Set DefTable = DefDatabase.CreateTableDef("中國(guó)")
    'dbBinary = 9
    'dbBoolean = 1
    'dbByte = 2
    'dbChar=18
    'dbDate=8
    'dbInteger=3
    'dbLong=4
    'dbMemo=12
    'dbText=10
    '建立Name字段為8個(gè)字符型
    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個(gè)的常整型
    Set DefField = DefTable.CreateField("Age", dbInteger, 3)
    '字段追加
    DefTable.Fields.Append DefField
    '表追加
    DefDatabase.TableDefs.Append DefTable
    MsgBox " 一切 OK , 《中國(guó)》表已經(jīng)建立完成! ", vbInformation
    Exit Sub
    Err100:
    MsgBox "對(duì)不起,不能建立表。請(qǐng)先再建表前建立VB-CODE數(shù)據(jù)庫(kù)? ", vbCritical
    End Sub
    Private Sub cmdCreate_Click()
    On Error GoTo Err100
    '建立名為 VB-CODE 的數(shù)據(jù)庫(kù)
    CreateDatabase "VB-CODE", dbLangGeneral
    MsgBox " 一切 OK , 數(shù)據(jù)庫(kù)建立完成! ", vbInformation
    Exit Sub
    Err100:
    MsgBox "不能建立數(shù)據(jù)庫(kù)! " & vbCrLf & vbCrLf & Err.Description, vbInformation
    End Sub