|
请参考:
Sub setDatabase()
Dim CatlogObject As New ADOX.Catalog
Dim Mypath As String
Dim myTable As String
Dim SQL As String
Mypath = ThisWorkbook.Path & "\测试数据库.mdb"
myTable = "测试数据表"
If Dir(Mypath) <> "" Then Kill Mypath
CatlogObject.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Mypath
' CatlogObject.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Mypath & ";Jet OLEDB:Database Password=123;" '带密码
'以下是创建数据表,可选
SQL = "CREATE TABLE " & myTable _
& "(员工编号 long not null primary key,姓名 text(20) not null," _
& "性别 text(1) not null,民族 text(20) not null,部门 text(20) not null,职务 text(20)," _
& "电话 text(20),学历 text(20),出生日期 date not null,籍贯 text(20),简历 longtext,照片 longbinary)"
CatlogObject.ActiveConnection.Execute SQL
MsgBox "创建数据库成功!" & vbCrLf _
& "数据库文件名为:" & Mypath & vbCrLf & "数据表名称为:" & myTable & vbCrLf _
& "保存位置:" & ThisWorkbook.Path, vbOKOnly + vbInformation, "创建数据库"
Set CatlogObject = Nothing
End Sub
下面程序用来测试新建数据库文件中的表信息
Sub getTables() '获取数据库所有表的信息
Dim CatlogObject As New ADOX.Catalog
Dim myTb As ADOX.Table
Dim Mypath As String
Dim i As Integer
Mypath = ThisWorkbook.Path & "\测试数据库.mdb"
CatlogObject.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Mypath
' CatlogObject.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Mypath & ";Jet OLEDB:Database Password=123;" '带密码
Cells.ClearContents
Cells(1, 1) = "数据表名"
Cells(1, 2) = "类型"
i = 1
For Each myTb In CatlogObject.Tables
i = i + 1
Cells(i, 1) = myTb.Name
Cells(i, 2) = myTb.Type
Next
Set myTb = Nothing
Set CatlogObject = Nothing
End Sub
|
|