VBA-数据库创建,数据表创建

1.首先我们要判断数据库是否存在,需要引用一个类“Microsoft ADO Ext.6.0 for DDL and Security”,用其catalog 对象来判断数据库是否存在,如果不存在,直接创建数据库和数据表,如果存在则删除

Option Explicit
Sub shifuchunzai()
    Dim con As New ADODB.Connection
    Dim mycat As New ADOX.Catalog
    Dim mydata As String
    Dim sql As String
    mydata = ThisWorkbook.Path & "\成绩管理.accdb" '指定数据库名称
    Dir (mydata) 'go,去找括号里的数据库
   ' MsgBox Dir(mydata)
   '利用dir 函数可以判断某个文件是否存在
    If Len(Dir(mydata)) > 0 Then
        MsgBox "数据库已存在"
       ' kill mydata '删除数据库
        Exit Sub
    End If
    mycat.Create "provider=microsoft.ace.oledb.12.0;data source=" & mydata
    With con
        .Provider = "microsoft.ace.oledb.12.0"
        .Open mydata
    End With
    '创建数据表的SQL命令
    'create table 表名(字段 类型(宽度)约束条件)
    sql = "create table 期中成绩(学号 text(10) not null," _
        & " 姓名 text(8) not null,性别 text(1) not null," _
        & " 班级 text(8) not null,语文 single not null," _
        & " 数学 single not null,英语 single not null," _
        & " 物理 single not null,化学 single not null," _
        & " 生物 single not null,总分 single not null)"
     con.Execute sql
     MsgBox "数据库创建成功", vbInformation, "创建数据库"
     con.Close
     Set con = Nothing
     Set mycat = Nothing
     
    
End Sub

2.在已有的数据库中创建表格以下有两种方法,一种是用command方法创建,一种是用SQL语句创建

Option Explicit
Sub 利用command对象创建表()
    On Error Resume Next
    Dim mycmd As New ADODB.Command
    Dim mycat As New ADOX.Catalog
    Dim mydata As String
    Dim mytable As String
    Dim sql As String
    mydata = ThisWorkbook.Path & "\成绩管理.accdb"
    mytable = "期末成绩"
    '建立数据库连接
    mycat.ActiveConnection = "provider=microsoft.ace.oledb.12.0;data source=" & mydata
    '删除已存在的同名数据表
    mycat.Tables.Delete mytable
    '设置数据库连接,方便接下来创建数据表
    Set mycmd.ActiveConnection = mycat.ActiveConnection
    sql = "create table 期末成绩(学号 text(10) not null," _
        & " 姓名 text(8) not null,性别 text(1) not null," _
        & " 班级 text(8) not null,语文 single not null," _
        & " 数学 single not null,英语 single not null," _
        & " 物理 single not null,化学 single not null," _
        & " 生物 single not null,总分 single not null)"
    '利用command 对象的execute 方法执行命令
    With mycmd
        .CommandText = sql
        .Execute , , adCmdText '表示执行一个文本字符串命令
    End With
    MsgBox "数据表创建成功", vbInformation, "创建数据表"
    Set mycmd = Nothing
    Set mycat = Nothing
    
End Sub
Sub 利用SQL语言创建数据表()
    Dim con As New ADODB.Connection
    Dim mydata As String
    Dim mytable As String
    Dim sql As String
    mydata = ThisWorkbook.Path & "\成绩管理.accdb"
    mytable = "期末成绩"
    '建立数据库连接
    With con
        .Provider = "microsoft.ace.oledb.12.0"
        .Open mydata
    End With
    '删除已有的同名数据表:drop table 表名
    sql = "drop table " & mytable '注意空格
    con.Execute sql
    '设置创建数据标的SQL语句
    sql = "create table 期末成绩(学号 text(10) not null," _
        & " 姓名 text(8) not null,性别 text(1) not null," _
        & " 班级 text(8) not null,语文 single not null," _
        & " 数学 single not null,英语 single not null," _
        & " 物理 single not null,化学 single not null," _
        & " 生物 single not null,总分 single not null)"
    '利用connection对象的execute 方法执行命令
    con.Execute sql
    con.Close
    Set con = Nothing
    MsgBox "数据表创建成功!", vbInformation, "创建数据表"
    

End Sub

 

你可能感兴趣的:(VBA)