用VB的代码创建Access数据库
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
怎样用VB的代码创建Access数据库
用 ADOX:
'菜单“工程”--> "引用 "--> Microsoft ADO Ext.2.7 for DDL ado Security ' 建数据库:
Private Sub Form_Load()
Dim cat As ADOX.Catalog
Set cat = New ADOX.Catalog
'在当前目录下建立名为newdata的Access数据库
cat.Create ( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + App.Path & "\newdata.mdb " + "; ")
MsgBox "数据库已经创建成功! "
End Sub
' 建表:
Private Sub Command1_Click()
Dim cn As New ADODB.Connection
cn.ConnectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "newdata.mdb;Persist Security Info=False "
cn.Open
cn.Execute "CREATE TABLE [aaa]([学生姓名]Text(20),[年龄]Integer,[成绩]Double) "
cn.Close
End Sub
'删表:
Private Sub Command2_Click()
Dim cn As New ADODB.Connection
cn.ConnectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source== " & App.Path & "newdata.mdb;Persist Security Info=False "
cn.Open
cn.Execute "DROP TABLE [aaa] "
cn.Close
End Sub
///////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////Option Explici t
Dim i As Integer
Dim j As Integer
Private Sub Form_Load()
'菜单“工程”--> "引用 "--> "Microsoft ActiveX Data Objects 2.7 Library " 'Microsoft ADO Ext.2.7 for DDL ado Security
Dim cat As ADOX.Catalog
Set cat = New ADOX.Catalog
cat.Create ( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\newdata.mdb " + "; ")
MsgBox "数据库已经创建成功! "
Dim tbl As ADOX.Table
Set tbl = New ADOX.Table
tbl.ParentCatalog = cat
= "MyTable "
'增加一个自动增长的字段
Dim col As ADOX.Column
Set col = New ADOX.Column
col.ParentCatalog = cat
col.Type = ADOX.DataTypeEnum.adInteger ' // 必须先设置字段类型
= "id "
col.Properties( "Jet OLEDB:Allow Zero Length ").Value = False
col.Properties( "AutoIncrement ").Value = True
tbl.Columns.Append col, ADOX.DataTypeEnum.adInteger, 0
'增加一个文本字段
Dim col2 As ADOX.Column
Set col2 = New ADOX.Column
col2.ParentCatalog = cat
= "Description "
col2.Properties( "Jet OLEDB:Allow Zero Length ").Value = False
tbl.Columns.Append col2, ADOX.DataTypeEnum.adVarChar, 25
'增加一个货币型字段
Dim col4 As ADOX.Column
Set col4 = New ADOX.Column
col4.ParentCatalog = cat
col4.Type = ADOX.DataTypeEnum.adCurrency
= "xx "
tbl.Columns.Append col4, ADOX.DataTypeEnum.adCurrency
'增加一个OLE字段
Dim col5 As ADOX.Column
Set col5 = New ADOX.Column
col5.ParentCatalog = cat
col5.Type = ADOX.DataTypeEnum.adLongVarBinary
= "OLD_FLD "
tbl.Columns.Append col5, ADOX.DataTypeEnum.adLongVarBinary
'增加一个数值型字段